daily pastebin goal
92%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require("data.table")
  2. dt <- data.table(id=c(1,1,2,2),
  3.                   event=rep(c("start", "end"), times=2),
  4.                   time=c(as.POSIXct(c("2014-01-31 06:05:30",
  5.                                       "2014-01-31 06:45:30",
  6.                                       "2014-01-31 08:10:00",
  7.                                       "2014-01-31 09:30:00"))))
  8. dt$time[2] - dt$time[1]  # in minutes
  9. dt$time[4] - dt$time[3]  # in hours
  10. dt[ , max(time) - min(time), by=id]  # wrong units printed for id 2
  11.    
  12. dt[ , difftime(max(time), min(time), units="mins"), by=id]  # both in mins
  13. dt[ , difftime(max(time), min(time), units="hours"), by=id]  # both in hours
  14.    
  15. dt[ , list(c(max(time) - min(time)),attr(max(time) - min(time),"units")), by=id]
  16.    
  17. dt[ , list(c(d<-max(time) - min(time)), attr(d,"units")), by=id]
  18.    id        V1    V2
  19. 1:  1 40.000000  mins
  20. 2:  2  1.333333 hours
  21.    
  22. dt[ , as.numeric( max(time) - min(time), units="hours" ), by=id]
  23.    
  24. dt[ , difftime(max(time), min(time), units = 'mins'), by = id]
  25. #    id      V1
  26. # 1:  1 40 mins
  27. # 2:  2 80 mins
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top