ggplot2 - Incrementally add seconds of a timestamp column grouped by ID in R -


i have dataframe time series data.

    timestamp <- c("1/27/2015 18:28:16","1/27/2015 18:28:17","1/27/2015 18:28:19","1/27/2015 18:28:20","1/27/2015 18:28:23","1/28/2015 22:43:08","1/28/2015 22:43:09","1/28/2015 22:43:13","1/28/2015 22:43:15","1/28/2015 22:43:16" )      id <- c("a","a","a","a","a","b","b","b","b","b")     v1<- c(1.70,1.71,1.77,1.79,1.63,7.20,7.26,7.16,7.18,7.18)      df <- data.frame(timestamp ,id,v1)  timestamp            id             v1 1/27/2015 18:28:16                1.70 1/27/2015 18:28:17                1.71 1/27/2015 18:28:19                1.77 1/27/2015 18:28:20                1.79 1/27/2015 18:28:23                1.63 1/28/2015 22:43:08   b              7.20 1/28/2015 22:43:09   b              7.26 1/28/2015 22:43:13   b              7.16 1/28/2015 22:43:15   b              7.18 1/28/2015 22:43:16   b              7.18 

since dont care timestamp, thinking of creating column called interval plot data in 1 plot.

i wrongly creating interval column doing this

df$interval <- cut(df$timestamp, breaks="sec") 

i want incrementally add "secs" of timestamp , put in interval column , should grouped id. mean, everytime has new id, interval column resets 1 , incrementally adds timestamp (secs).

my desired output

timestamp            id             v1      interval 1/27/2015 18:28:16                1.70      1 1/27/2015 18:28:17                1.71      2 1/27/2015 18:28:19                1.77      4 1/27/2015 18:28:20                1.79      5 1/27/2015 18:28:23                1.63      8 1/28/2015 22:43:08   b              7.20      1 1/28/2015 22:43:09   b              7.26      2 1/28/2015 22:43:13   b              7.16      6  1/28/2015 22:43:15   b              7.18      8 1/28/2015 22:43:16   b              7.18      9 

i plot using ggplot interval vs v1 id , 2 time series in same plot. extract features it.

please me how work around problem can apply larger dataset.

one solution data.table:

for data:

library(data.table) df <- as.data.table(df) df$timestamp <- as.posixct(df$timestamp, format='%m/%d/%y %h:%m:%s') df[, interval := as.numeric(difftime(timestamp, .sd[1, timestamp], units='secs') + 1)   , by=id] 

which outputs:

> df               timestamp id   v1 interval  1: 2015-01-27 18:28:16  1.70        1  2: 2015-01-27 18:28:17  1.71        2  3: 2015-01-27 18:28:19  1.77        4  4: 2015-01-27 18:28:20  1.79        5  5: 2015-01-27 18:28:23  1.63        8  6: 2015-01-28 22:43:08  b 7.20        1  7: 2015-01-28 22:43:09  b 7.26        2  8: 2015-01-28 22:43:13  b 7.16        6  9: 2015-01-28 22:43:15  b 7.18        8 10: 2015-01-28 22:43:16  b 7.18        9 

then ggplot:

library(ggplot2) ggplot(df, aes(x=interval, y=v1, color=id)) + geom_line() 

and graph:

enter image description here


Comments