Nell
Nell

Reputation: 559

time difference between date[i + 1] and date[i] by group

I would like to calculate time difference (in hours) between tab$date[i + 1] and tab$date[i] by group. Here my code:

setDT(tab)
system.time(tab <- tab[ , diffTime := c(0,diff(date, units="hours")), by="ID"])
tab$diffTime <- round(tab$diffTime)

The problem is that I obtain both hours, minutes, and seconds:

Date                   DiffTime
2012-03-05 01:00:36    0
2012-03-05 03:00:35    2
2012-03-05 05:01:05    2
...
2010-01-29 21:01:00    0
2010-01-29 22:01:01    60
2010-01-29 23:01:12    60
...
2012-02-13 05:00:34    0
2012-02-13 16:01:06    39632
2012-02-14 03:00:47    39581

The dates are POSIXct data. How can I obtain only hours ?

Upvotes: 3

Views: 470

Answers (1)

akrun
akrun

Reputation: 887213

We could use difftime and specify the units as 'hours'.

library(data.table)
setDT(tab)[, DiffTime := c(0, round(difftime(date[-1L], date[-.N], 
                                 units='hours'))), by= ID]
tab
#   ID                date DiffTime
#1:  1 2012-03-05 01:00:36        0
#2:  1 2012-03-05 03:00:35        2
#3:  1 2012-03-05 05:01:05        2
#4:  2 2010-01-29 21:01:00        0 
#5:  2 2010-01-29 22:01:01        1
#6:  2 2010-01-29 23:01:12        1

data

tab <- data.frame(ID= rep(1:2, each=3),
  date= as.POSIXct(c('2012-03-05 01:00:36', '2012-03-05 03:00:35', 
  '2012-03-05 05:01:05', '2010-01-29 21:01:00', '2010-01-29 22:01:01',
  '2010-01-29 23:01:12'), format='%Y-%m-%d %H:%M:%S'))

Upvotes: 4

Related Questions