I would like to expand a data frame in R based on a datetime column in POSIXct format. Each row of datetimes (column[1]) in my data frame currently represents the start of a time block. The length of the time block in seconds is given in column[2]. I would like to expand the data frame to give a separate time stamp (row) for each second in that time block, as specified in column 2.
Here is some example data:
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))
This is what I currently have:
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
This is what I would like to end up with:
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
I am having trouble finding a simple way to perform this processing task, and answers to similar questions do not provide me with a solution to this problem (i.e. 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). I’m thinking that functions like split()
, merge()
, and ddply()
might help, but I can’t work it out. I am still learning, so any suggestions would be greatly appreciated.