我正在使用data.table相当多.它工作得很好,但我发现转换我的语法花了很长时间才能利用二进制搜索.
在下面的数据表中,1将如何选择所有行,包括CPT值为NA的位置,但排除CPT值为23456或10000的行.
cpt <- c(23456,23456,10000,44555,44555,NA)
description <- c("tonsillectomy","tonsillectomy in >12 year old","brain transplant","castration","orchidectomy","miscellaneous procedure")
cpt.desc <- data.table(cpt,description)
setkey(cpt.desc,cpt)
以下行有效,但我认为它使用矢量扫描方法而不是二进制搜索(或二进制排除).有没有办法通过二进制方法删除行?
cpt.desc[!cpt %in% c(23456,10000),]
最佳答案 只是部分答案,因为我是data.table的新手.自联接适用于数字,但字符串也是如此.我相信其中一个专业数据计算器知道该怎么做.
library(data.table)
n <- 1000000
cpt.desc <- data.table(
cpt=rep(c(23456,23456,10000,44555,44555,NA),n),
description=rep(c("tonsillectomy","tonsillectomy in >12 year old","brain transplant","castration","orchidectomy","miscellaneous procedure"),n))
# Added on revision. Not very elegant, though. Faster by factor of 3
# but probably better scaling
setkey(cpt.desc,cpt)
system.time(a<-cpt.desc[-cpt.desc[J(23456,45555),which=TRUE]])
system.time(b<-cpt.desc[!(cpt %in% c(23456,45555))] )
str(a)
str(b)
identical(as.data.frame(a),as.data.frame(b))
# A self-join works Ok with numbers
setkey(cpt.desc,cpt)
system.time(a<-cpt.desc[cpt %in% c(23456,45555),])
system.time(b<-cpt.desc[J(23456,45555)])
str(a)
str(b)
identical(as.data.frame(a),as.data.frame(b)[,-3])
# But the same failes with characters
setkey(cpt.desc,description)
system.time(a<-cpt.desc[description %in% c("castration","orchidectomy"),])
system.time(b<-cpt.desc[J("castration","orchidectomy"),])
identical(as.data.frame(a),as.data.frame(b)[,-3])
str(a)
str(b)