jester
jester

Reputation: 309

how to find time difference from row names?

I have a data frame object with row names that are time stamps. "SIZE" is the only variable so far.

                           SIZE
2008-01-14 09:29:49         0
2008-01-14 09:29:59         0
2008-01-14 09:29:59.1       0
2008-01-14 09:30:00        842
2008-01-14 09:30:00.1      34
2008-01-14 09:30:00.2       1
2008-01-14 09:30:00.3       1
2008-01-14 09:30:00.4       1
2008-01-14 09:30:00.5       1
2008-01-14 09:30:02        38

I would like to create a new column that shows the time difference from the previous row. The time difference from the first row can be blank.

                        SIZE  dtime
2008-01-14 09:29:49      0     NA
2008-01-14 09:29:59      0     10 
2008-01-14 09:29:59.1    0     0.1
2008-01-14 09:30:00    842     0.9
2008-01-14 09:30:00.1   34     0.1
2008-01-14 09:30:00.2    1     0.1
2008-01-14 09:30:00.3    1     0.1
2008-01-14 09:30:00.4    1     0.1
2008-01-14 09:30:00.5    1     0.1
2008-01-14 09:30:02     38     1.5

I am trying to use a loop to solve it like:

file<-as.data.frame(mtq)
L<-nrow(file)
file$dtime<-NA
for (f in 2:L){

  file$dtime[f]<-difftime(row.names(file)[f], row.names(file)[f-1])
}

It has a error of "replacement has 12853 rows, data has 12852"

Would anyone please give me some advice?

Thank you

Upvotes: 2

Views: 59

Answers (1)

akrun
akrun

Reputation: 886938

You could use diff after converting to POSIXct. The length of the diff output will be one less than than nrow of the dataset. So, we can append NA at the beginning.

 df1$dtime <- c(NA,round(diff(as.POSIXct(row.names(df1))),1))
 df1
 #                      SIZE dtime
 #2008-01-14 09:29:49      0    NA
 #2008-01-14 09:29:59      0  10.0
 #2008-01-14 09:29:59.1    0   0.1
 #2008-01-14 09:30:00    842   0.9
 #2008-01-14 09:30:00.1   34   0.1
 #2008-01-14 09:30:00.2    1   0.1
 #2008-01-14 09:30:00.3    1   0.1
 #2008-01-14 09:30:00.4    1   0.1
 #2008-01-14 09:30:00.5    1   0.1
 #2008-01-14 09:30:02     38   1.5

Or

 v1 <- as.POSIXct(row.names(df1))
 df1$dtime <- round(v1-lag(v1),1)

Upvotes: 2

Related Questions