What is the optimal way to setkey
the data.table with reversed order of the records? So far I use the combination of setkey()
and setorder()
:
setkeyrev <- function(inputDT,...){
setkey(inputDT, ...)
setorderv(inputDT, key(inputDT), order = -1)
invisible(inputDT)
}
Is there a better solution?
UPD. here is an example:
myDT <- fread('
colA colB
1 b1
3 b3
8 b8
5 b5')
setkey(myDT, colA)
myDT
setkeyrev(myDT, colA)
myDT