【发布时间】:2013-07-18 10:49:07
【问题描述】:
我想根据 POSIXct 格式的日期时间列在 R 中扩展数据框。我的数据框中的每一行日期时间(列 [1])当前代表一个时间块的开始。以秒为单位的时间块长度在列 [2] 中给出。我想扩展数据框,为该时间块中的每一秒提供一个单独的时间戳(行),如第 2 列中所述。
以下是一些示例数据:
structure(list(datetime = structure(1:5, .Label = c("14/04/2013 17:42:29",
"14/04/2013 17:43:49", "14/04/2013 17:43:58", "14/04/2013 17:44:03",
"14/04/2013 17:44:11"), class = "factor"), duration = c(1L, 5L,
2L, 3L, 2L), mean = c(1.17, 2.36, 1.05, 1.43, 1.47)), .Names = c("datetime",
"duration", "mean"), class = "data.frame", row.names = c(NA,
-5L))
这是我目前拥有的:
datetime duration mean
14/04/2013 17:42:29 1 1.17
14/04/2013 17:43:49 5 2.36
14/04/2013 17:43:58 2 1.05
14/04/2013 17:44:03 3 1.43
14/04/2013 17:44:11 2 1.47
这就是我想要的结果:
datetime duration mean
14/04/2013 17:42:29 1 1.17
14/04/2013 17:43:49 1 2.36
14/04/2013 17:43:50 1 2.36
14/04/2013 17:43:51 1 2.36
14/04/2013 17:43:52 1 2.36
14/04/2013 17:43:53 1 2.36
14/04/2013 17:43:58 1 1.05
15/04/2013 17:43:59 1 1.05
14/04/2013 17:44:03 1 1.43
14/04/2013 17:44:04 1 1.43
14/04/2013 17:44:05 1 1.43
14/04/2013 17:44:11 1 1.47
14/04/2013 17:44:12 1 1.47
我很难找到一种简单的方法来执行此处理任务,并且对类似问题的回答无法为我提供解决此问题的方法(即How to convert 10-minute time blocks to 1-minute intervals in R、Expand Categorical Column in a Time Series to Mulitple Per Second Count Columns)。我认为split()、merge() 和ddply() 之类的函数可能会有所帮助,但我无法解决。我还在学习,所以任何建议将不胜感激。
【问题讨论】:
-
+1 是一个很好的可重现示例