Lets say I have a data table DT and I change the ordering with set key
setkey(DT,mykey)
Then, maybe I join some things from another table.
DT=DT2[DT]
Is there any way to recover my original row ordering? I know, I can do it by explicitly including an index before I use setkey.
N=Nrow(DT)
DT[,orig_index:=1:N]
setkey(DT,mykey)
DT=DT2[DT]
setkey(DT,orig_index)
DT[,orig_index:=NULL]
Is there a simpler way? If I was doing this with order instead of set key, this would be a little simpler.
o=order(DT$mykey)
uo=order(o)
setkey(DT,mykey)
DT=DT2[DT]
DT=DT[uo,]
It would be kind cool I guess if setkey could be reversed with something like this
setkey(DT,mykey,save.unset=T)
DT=DT2[DT]
unsetkey(DT)
Here save.unset=T would tell data.table to save the last reordering so it can be reversed.
Better yet, maybe
setkey(DT, reorder=F)
DT=DT2[DT]
This option would tell data.table to use the key ordering for joins or whatever without actually changing the order of DT. Not sure if that is possible or natural to implement.
Description. setkey sorts a data. table and marks it as sorted with an attribute sorted . The sorted columns are the key. The key can be any number of columns.
To sort a data frame in R, use the order( ) function. By default, sorting is ASCENDING. Prepend the sorting variable by a minus sign to indicate DESCENDING order.
Agreed. This is what we're calling a secondary key and the plan is to add set2key
to do exactly that. It is possible to do manual secondary keys now. But that's very similar to what you have in the question. It has come up quite a lot.
FR#1007 Build in secondary keys
and some examples :
https://stackoverflow.com/a/13660454/403310
https://stackoverflow.com/a/13969805/403310
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With