Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require("data.table")
- dt <- data.table(id=c(1,1,2,2),
- event=rep(c("start", "end"), times=2),
- time=c(as.POSIXct(c("2014-01-31 06:05:30",
- "2014-01-31 06:45:30",
- "2014-01-31 08:10:00",
- "2014-01-31 09:30:00"))))
- dt$time[2] - dt$time[1] # in minutes
- dt$time[4] - dt$time[3] # in hours
- dt[ , max(time) - min(time), by=id] # wrong units printed for id 2
- dt[ , difftime(max(time), min(time), units="mins"), by=id] # both in mins
- dt[ , difftime(max(time), min(time), units="hours"), by=id] # both in hours
- dt[ , list(c(max(time) - min(time)),attr(max(time) - min(time),"units")), by=id]
- dt[ , list(c(d<-max(time) - min(time)), attr(d,"units")), by=id]
- id V1 V2
- 1: 1 40.000000 mins
- 2: 2 1.333333 hours
- dt[ , as.numeric( max(time) - min(time), units="hours" ), by=id]
- dt[ , difftime(max(time), min(time), units = 'mins'), by = id]
- # id V1
- # 1: 1 40 mins
- # 2: 2 80 mins
Add Comment
Please, Sign In to add comment