将数据帧转换为30分钟间隔的时间序列

时间:2012-10-01 22:12:24

标签: r

使用

x <- data.frame(value = round(rt(20, 2), 2),
                time = c("2012-09-03 00:00:00", "2012-09-03 00:30:00",
                         "2012-09-03 01:00:00", "2012-09-03 01:30:00",
                         "2012-09-03 02:00:00", "2012-09-03 02:30:00",
                         "2012-09-03 03:00:00", "2012-09-03 03:30:00",
                         "2012-09-03 04:00:00", "2012-09-03 04:30:00",
                         "2012-09-03 05:00:00", "2012-09-03 05:30:00",
                         "2012-09-03 06:00:00", "2012-09-03 06:30:00",
                         "2012-09-03 07:00:00", "2012-09-03 07:30:00",
                         "2012-09-03 08:00:00", "2012-09-03 08:30:00", 
                         "2012-09-03 09:00:00", "2012-09-03 09:30:00"))

有没有办法将它转换为30分钟间隔的时间序列格式?如:

           00:00:00 00:30:00 01:00:00 01:30:00 02:00:00 02:30:00 ...
2012-09-03     0.31    -1.01     0.94    -0.75    -0.40     0.41

事实上,有一天以上的数据,我只是展示了它的一部分。谢谢!

1 个答案:

答案 0 :(得分:2)

library(plyr) 
library(reshape2)
x2 <- cbind(x, ldply(strsplit(x$time, " ")))[-2]
names(x2) <- c("value", "date", "time")
dcast(x2, date~time)