0

I would like to sort my data table after time using setorder.

library(data.table)
DT <- data.table(timestamp=c(as.POSIXct("2013-01-01 17:54:23.577"),
                             as.POSIXct("2013-01-01 17:54:23.568"),
                             as.POSIXct("2013-01-01 17:54:23.909"),
                             as.POSIXct("2013-01-01 17:54:23.901")))
setorder(DT,timestamp)
print(DT)

Outputs:

                 timestamp
1: 2013-01-01 17:54:23.568
2: 2013-01-01 17:54:23.576
3: 2013-01-01 17:54:23.908
4: 2013-01-01 17:54:23.901

Setorder fails to correctly sort the data table and I am not sure why. Is it possible to somehow increase the sensitivity or any other possible solution?

johnblund
  • 402
  • 5
  • 21
  • 3
    Q is different but answer is the same, as shown [here](http://stackoverflow.com/a/37946500/559784). The plan is to not round by default from next release on. – Arun Jul 19 '16 at 14:35

1 Answers1

3

From the help file:

Columns of numeric types (i.e., double) have their last two bytes rounded off while computing order, by defalult, to avoid any unexpected behaviour due to limitations in representing floating point numbers precisely. Have a look at setNumericRounding to learn more.

So this is likely due to the rounding of these columns during the sorting process. You can adjust this to not round using

setNumericRounding(0)
lmo
  • 37,904
  • 9
  • 56
  • 69