如何撤消data.table中的setkey顺序? [英] How do you undo a setkey ordering in data.table?
问题描述
假设我有一个数据表DT,我用set键更改顺序
setkey(DT,mykey)
然后,也许我从另一个表中加入一些东西。
DT = DT2 [DT]
任何方式恢复我的原始行排序?我知道,我可以通过在使用setkey之前明确包含一个索引。
N = Nrow(DT)
DT [,orig_index:= 1:N]
setkey DT,mykey)
DT = DT2 [DT]
setkey(DT,orig_index)
DT [,orig_index:= NULL]
有更简单的方法吗?如果我用order而不是set key来做这个,这将会更简单一些。
o = order )
uo = order(o)
setkey(DT,mykey)
DT = DT2 [DT]
DT = DT [uo,]
这很酷我想如果setkey可以颠倒像这样
setkey(DT,mykey,save.unset = T)
DT = DT2 [DT]
unsetkey(DT)
这里save.unset = T会告诉data.table保存最后的重排序,所以可以反转。
更好的是,
setkey(DT,reorder = F)
DT = DT2 [DT]
这个选项会告诉data.table使用键的顺序,改变DT的顺序。不确定这是可能还是自然实施。
解决方案同意。这是我们称之为辅助键,计划是添加
set2key
来做到这一点。现在可以做手动辅助键。但是,这与你在问题中的相似。它已经出现了很多。
http://stackoverflow.com/a/13660454/403310
http://stackoverflow.com/a/13969805/403310Lets 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.
解决方案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 :
http://stackoverflow.com/a/13660454/403310
http://stackoverflow.com/a/13969805/403310这篇关于如何撤消data.table中的setkey顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!