When durations are computed in data.table (v1.9.2), the wrong units can be printed with POSIXct arithmetic. It seems the first units are chosen.
require(\"da
You'll get the expected result if you do
dt[ , list(c(max(time) - min(time)),attr(max(time) - min(time),"units")), by=id]
Putting the c() around the time operation eliminates the attribute so you just get a number and then explicitly asking for the "units" attribute as another list element by itself gets the proper unit in its own column. The reason it doesn't work without doing it this way is that data.table doesn't parse out attributes to be other columns and that is how POSIXct returns the units.
From Matt:
+1 Just to add a small speed improvement to save the max(time)-min(time) twice :
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
At least to start with, I guess we'll add a check for inconsistent attributes across group results and then issue a warning/error. So this solution (or the one in the question) will likely be needed anyway.