14

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.

Matt Dowle
  • 58,872
  • 22
  • 166
  • 224
user1827975
  • 427
  • 3
  • 10
  • 1
    I am not aware of a way to do this as such in data.table. I remember a similar feature request on *no automatic reordering of key columns* and Matthew taking it happily as a feature request. So, I would suppose Matthew, when he checks this post, would not mind adding this as a feature request (unless there's a specific reason). I think this feature may come in handy at times too. – Arun Mar 21 '13 at 16:08

1 Answers1

7

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

Community
  • 1
  • 1
Matt Dowle
  • 58,872
  • 22
  • 166
  • 224