Wrong units displayed in data.table with POSIXct arithmetic

前端 未结 3 716
后悔当初
后悔当初 2021-01-20 16:35

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         


        
3条回答
  •  北海茫月
    2021-01-20 17:19

    Forcing units is the way to go until #761 is fixed. Here's another option:

    dt[ , difftime(max(time), min(time), units = 'mins'), by = id]
    #    id      V1
    # 1:  1 40 mins
    # 2:  2 80 mins
    

    This allows you to retain the class of the output (difftime) if you'd like.

    More than anything I find the behavior of R to fundamentally change the contents of the difftime object based on the units attribute quite strange. In other places in R, this conversion would simply be handled by the print method while the stored value of the object remains consistent.

提交回复
热议问题