我想根据其他地方的变量集选择data.table中的行,但我缺少一些关键的语法点。
library(data.table)
DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
DT
#this works as expected and does what I want
DT[y<3 & v >3]
#but how do I pass my selection criteria in?
a1<-"y<3 & v>3"
a2<-quote(y<3 & v>3)
#so that one of these works?
DT[a1] #error
DT[a2] #error
DT[eval(a1)] #error
答案 0 :(得分:3)
......最后一次测试可以做到......
library(data.table)
DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
DT
#this works as expected and does what I want
DT[y<3 & v >3]
#but how do I pass my selection criteria in?
a1<-"y<3 & v>3"
a2<-quote(y<3 & v>3)
#so that one of these works?
DT[a1] #error
DT[a2] #error
DT[eval(a1)] #error
DT[eval(a2)] #works