4

我正在使用 data.table 包。有很多使用二分搜索的子集、查询或搜索(或任何你想称之为的)的例子,这显然比矢量扫描快得多。这是帮助文件的摘录。

DT["a"]                    # binary search (fast)
DT[x=="a"]                 # vector scan (slow)

但是,如果要搜索的列不是一个因素(或字符)而是一个整数,会发生什么。

cpt <- c(23456,23456,10000,44555,44555)
description <- c("tonsillectomy","tonsillectomy in >12 year old","brain transplant","castration","orchidectomy")
cpt.desc <- data.table(cpt,description)

setkey(cpt.desc,cpt)
cpt.desc[10000,]  

这不起作用,因为整数 10000 被解释为该 data.table 中不存在的第 10000 行。

如果我们修改语法,那么我们就会得到我们正在寻找的东西。

cpt.desc[cpt==10000,]

但是,这看起来很像是慢速矢量扫描方法。data.table 包中是否有整数的二进制搜索功能?感谢您期待您的帮助。

4

1 回答 1

4

试试cpt.desc[J(10000)]。添加,"mult=all"以获取所有匹配项。

于 2012-01-18T22:07:53.650 回答