I\'m trying to figure out the best way how to use multiple variable filters in R.
Usually have up to 100 variables (in one condition) and need to filter cases where ANY
We could use Reduce
with lapply
vys1 <- dt[, Reduce('|', lapply(.SD, '==', 37)), .SDcols= x1:x10]
identical(as.vector(vys), vys1)
#[1] TRUE
Based on the same sort of benchmarks used
pm<-proc.time()
vys<-((x1==37) | (x2==37) | (x3==37) | (x4==37) | (x5==37) | (x6==37) | (x7==37) | (x8==37) | (x9==37) | (x10==37))
proc.time() - pm
# user system elapsed
# 0.05 0.13 0.93
pm<-proc.time()
vys1 <- dt[, Reduce('|', lapply(.SD, '==', 37)), .SDcols= x1:x10]
proc.time() - pm
# user system elapsed
# 0.05 0.03 0.08