我想知道是否有任何有效的方法来过滤data.table中定义的多个条件的data.table。在这种情况下有2个data.table:如何通过另一个data.table中定义的多个条件过滤data.table中的个案
# the filter data.table defines the condition
dt_filter<-data.table(A=c(1,2),B=c(8,7))
# dt1 the data.table to be filtered
dt1<-data.table(A=rep(c(1,2),5),B=c(8,4,3,1,1,5,9,7,1,1),C=c(rep(1,5),rep(2,5)))
ls_tmp<-lapply (1:nrow(dt_filter),function(i){
# exclude the record with the A&B defined the filter
dt1_add<-dt1[A==dt_filter[[i,1]]&B!=dt_filter[[i,2]]]
})
result<- rbindlist(ls_tmp)
看来我的样本效率不高,因为lapply循环。我不知道如何以其他方式重写它。
什么是组?请标注“A,B,C”。 'B!= dt_filter [...'这里不一样,是错字吗? – Vlo 2015-02-24 20:31:22
@Vlo列A是组ID。 – YYY 2015-02-24 20:34:06
你的代码真的很难阅读。尝试清理和简化,为我们和你的! – 2015-02-24 20:37:29