-1
我有更大的一组数据,并且需要满足某些条件的行的行号。行号。打包data.table。列的子集上的data.table“二进制搜索”的行索引
days <- strptime(c("2013-01-01 8:00:00", "2013-02-01 8:00:00"), format="%Y-%m-%d %H:%M:%S")
DateTime <- rep(seq(days[1], days[2], length.out=1e6/5), 5)
Update <- rep(LETTERS[3:1], length.out=1e6)
Group <- rep(c("AAA", "BBB", "CCC"), length.out=1e6)
Weight <- trunc(rnorm(1e6, 110, 3))
Weight2 <- rnorm(1e6, 100, 1.5)
DT <- data.table(DateTime, Update, Group, Weight, Weight2)
setkey(DT, DateTime, Update, Group, Weight, Weight2)
Exp <- DT[1e6/2]
我不能没有列的DateTime创建另一个data.table作为一个子集,因为此列中的关键应用。在子集上创建一个新密钥可能会改变顺序,我需要确定原始顺序已被保留。
有可能通过使用两个命令来得到行号我所需要的。
system.time(DT[, which(DT$Update==Exp$Update & DT$Group==Exp$Group & DT$Weight==Exp$Weight & DT$Weight2==Exp$Weight2)])
system.time(which(DT$Update==Exp$Update & DT$Group==Exp$Group & DT$Weight==Exp$Weight & DT$Weight2==Exp$Weight2))
不过,我需要一个更快的方法来做到这一点。
谢谢你的任何建议。
请避免说明包装的一般性。当他们错了时,他们会让你的问题变得更长,并且会特别困惑。让它简单,**我有这个,我尝试过,我得到了这个,但我想得到这个**。 – agstudy
我编辑了我的问题。 [链接](http://stackoverflow.com/questions/15597685/subsetting-data-table-by-2nd-column-only-of-a-2-column-key-using-binary-search)确实提供了一个答案到一个不同的,但类似的问题。解决方案是不同的。 – camsique