我在日期框架中有一个序列号列:
1
2
3
4
5
6
7
8
9
10
.
.
216
我想自动创建这个列。基本上,我想提到第一个时间步骤(2015-05-23 00:00:00),之后它应该为每个时间步骤增加1小时,具体取决于需要多少小时(这里是216)
2015-05-23 00:00:00
2015-05-23 01:00:00
2015-05-23 02:00:00
2015-05-23 03:00:00
2015-05-23 04:00:00
.
.
2015-06-01 00:00:00
我该怎么做?
假设df$num
是您的变量,其长度为216
,则
start <- as.POSIXct("2015-05-23 00:00:00")
seq(start, start + length(df$num) * 60*60, by = "hour")
' timmedate ' + 'difftime'-sequence = ' timmedate '-sequence:
df <- data.frame( t = NA, n = 0:12 )
t0 <- timeDate( "2015-05-23 00:00:00", format="%Y-%m-%d %H:%M:%S" )
df$t <- t0 + as.difftime( 0:(nrow(df)-1), units="hours")
结果:> df
t n
1 2015-05-23 00:00:00 0
2 2015-05-23 01:00:00 1
3 2015-05-23 02:00:00 2
4 2015-05-23 03:00:00 3
5 2015-05-23 04:00:00 4
6 2015-05-23 05:00:00 5
7 2015-05-23 06:00:00 6
8 2015-05-23 07:00:00 7
9 2015-05-23 08:00:00 8
10 2015-05-23 09:00:00 9
11 2015-05-23 10:00:00 10
12 2015-05-23 11:00:00 11
13 2015-05-23 12:00:00 12