How do you undo a setkey ordering in data.table?

2020-08-10 08:07发布

问题:

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.

回答1:

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



标签: r data.table