2
votes

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.

2
+1 for a good reproducible exampleSimon O'Hanlon

2 Answers

2
votes

You could use lapply to create a data.frame for each segement then rbind all the results together at the end, like this...

res <- lapply( 1:nrow(df) , function(x){ data.frame(
    datetime = strptime( df[ x , 1 ] , format = "%d/%m/%Y %H:%M:%S" ) +  ( seq_len( df[ x , 2 ] ) - 1 ) ,
    duration = rep( 1 , df[ x , 2 ] ) ,
    mean = rep( df[ x , 3 ] ,  df[ x , 2 ] ) ) } )

do.call( rbind , res )
#             datetime duration mean
#1  2013-04-14 17:42:29        1 1.17
#2  2013-04-14 17:43:49        1 2.36
#3  2013-04-14 17:43:50        1 2.36
#4  2013-04-14 17:43:51        1 2.36
#5  2013-04-14 17:43:52        1 2.36
#6  2013-04-14 17:43:53        1 2.36
#7  2013-04-14 17:43:58        1 1.05
#8  2013-04-14 17:43:59        1 1.05
#9  2013-04-14 17:44:03        1 1.43
#10 2013-04-14 17:44:04        1 1.43
#11 2013-04-14 17:44:05        1 1.43
#12 2013-04-14 17:44:11        1 1.47
#13 2013-04-14 17:44:12        1 1.47
1
votes

There might be an easier way, but I hope this will be quite fast:

DF$datetime <- as.POSIXct(DF$datetime, format="%d/%m/%Y %H:%M:%S", tz="GMT")

inverse.rle2 <- function(values,lengths) {
  #conserve class and attributes
  #so it plays well with date-time classes
  class.values <- class(values)
  attributes.values <- attributes(values)

  res <- rep.int(values, lengths)

  #assign class and attributes
  class(res) <- class.values
  attributes(res) <- attributes.values
  res
}

#use the function by looping over the columns
DF2 <- do.call(cbind.data.frame, lapply(DF[,-2], inverse.rle2, lengths=DF[,2]))
#add seconds to runs
DF2$datetime <- DF2$datetime + 
                do.call(c,
                        tapply(c(0,diff(DF2$datetime)==0), 
                               DF2$datetime, cumsum))

#              datetime mean
#1  2013-04-14 17:42:29 1.17
#2  2013-04-14 17:43:49 2.36
#3  2013-04-14 17:43:50 2.36
#4  2013-04-14 17:43:51 2.36
#5  2013-04-14 17:43:52 2.36
#6  2013-04-14 17:43:53 2.36
#7  2013-04-14 17:43:58 1.05
#8  2013-04-14 17:43:59 1.05
#9  2013-04-14 17:44:03 1.43
#10 2013-04-14 17:44:04 1.43
#11 2013-04-14 17:44:05 1.43
#12 2013-04-14 17:44:11 1.47
#13 2013-04-14 17:44:12 1.47