Multiple variable filters in r

后端 未结 3 405
陌清茗
陌清茗 2021-01-24 07:53

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

3条回答
  •  后悔当初
    2021-01-24 08:06

    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 
    

提交回复
热议问题