datetime - Expand a time series by specified time lengths in R -


i expand data frame in r based on datetime column in posixct format. each row of datetimes (column[1]) in data frame represents start of time block. length of time block in seconds given in column[2]. expand data frame give separate time stamp (row) each second in time block, specified in column 2.

here 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 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 end 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 having trouble finding simple way perform processing task, , answers similar questions not provide me solution problem (i.e. how convert 10-minute time blocks 1-minute intervals in r, expand categorical column in time series mulitple per second count columns). i’m thinking functions split(), merge(), , ddply() might help, can’t work out. still learning, suggestions appreciated.

you use lapply create data.frame each segement rbind results @ end, 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 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -