转换为R中的一年中的日期和时间 [英] Convert to the day and time of the year in R
问题描述
> x< - c('5/5/2007','12/31/2007','1/2/2008')
> #转换为年(julian date) -
> strptime(x,%m /%d /%Y)$ yday + 1
[1] 125 365 2
我想知道如何做同样的事情,但随着时间的推移。但我还是得到一天没有时间。任何人都可以建议用日期和时间来查找日期的更好方法是什么?
> x1< - c('5/5/2007 02:00','12/31/2007 05:58','1/2/2008 16:25')
> #转换为年(julian date) -
> strptime(x1,%m /%d /%Y%H:%M)$ yday + 1
[1] 125 365 2
而不是这个结果,我想以十进制天数输出。例如,第一个例子是 125.0833333
等等。
非常感谢你。
你希望能够获得一天的数值,作为输出的一部分吗?如果是这样,这样的工作将会起作用:
test< - strptime(x1,%m /%d /%Y %H:%M)
(测试$ yday + 1)+(测试$ hour / 24)+(测试$ min /(24 * 60))
#[1] 125.083333 365.248611 2.684028
尽管这符合您的要求,我想删除 +1
可能更有意义:
(测试$ yday)+(测试$ hour / 24 )+(测试$ min /(24 * 60))
#[1] 124.083333 364.248611 1.684028
虽然我的感觉是令人讨厌的是,Dirk会显示,并向我展示如何使用 POSIXct
日期/时间表示。
以下是使用基本功能的这种答案:
mapply(julian,as .POSIXct(test),paste(format(test,%Y),01,01,sep = - ))
#[1] 124.083333 364.248611 1.684028
I have data for more than 3 years. For each year I want to find the day corresponding to Jaunary 1 of that year. For example:
> x <- c('5/5/2007','12/31/2007','1/2/2008')
> #Convert to day of year (julian date) –
> strptime(x,"%m/%d/%Y")$yday+1
[1] 125 365 2
I want to know how to do the same thing but with time added. But I still get the day not time. Can anyone suggest what is the better way to find the julian date with date and time ?
> x1 <- c('5/5/2007 02:00','12/31/2007 05:58','1/2/2008 16:25')
> #Convert to day of year (julian date) –
> strptime(x1,"%m/%d/%Y %H:%M")$yday+1
[1] 125 365 2
Rather than this result, I want the output in decimal days. For example the first example would be 125.0833333
and so on.
Thank you so much.
Are you hoping to get the day + a numerical part of a day as output? If so, something like this will work:
test <- strptime(x1,"%m/%d/%Y %H:%M")
(test$yday+1) + (test$hour/24) + (test$min/(24*60))
#[1] 125.083333 365.248611 2.684028
Although this matches what you ask for, I think removing the +1
might make more sense:
(test$yday) + (test$hour/24) + (test$min/(24*60))
#[1] 124.083333 364.248611 1.684028
Though my spidey senses are tingling that Dirk is going to show up and show me how to do this with a POSIXct
date/time representation.
Here is an attempt of such an answer using base functions:
mapply(julian, as.POSIXct(test), paste(format(test,"%Y"),"01","01",sep="-"))
#[1] 124.083333 364.248611 1.684028
这篇关于转换为R中的一年中的日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!