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.