Search code examples
rdata.tablesubsetgrepl

How to set AND "&" multi conditions to remove rows with "!" data.table way R


Generally remove expression of data.table works, DT[!grepl("XXX",COLUMN),]

Target : Remove the rows with 3 conditions

# Like : grepl("BB",RR) & grepl("XLKG",B) & grepl("GA",FF)
  RR    B FF
  1: AA  XLJ KA
  2: BB XLKG CA
  3: BB XLKG GA     <----- remove this rows
  4: BB XLKG FA
  5: BB XLCC GA   

# DATASET
DD <- data.table(RR=c("AA","BB","BB","BB","BB"),B=c("XLJ","XLKG","XLKG","XLKG","XLCC"),FF=c("KA","CA","GA","FA","GA"))

Problem : The script below cannot apply multi remove expression

DD[!grepl("BB",RR) & grepl("XLKG",B) & grepl("GA",FF),][]
Empty data.table (0 rows) of 3 cols: RR,B,FF

How can i achieve that ? Using grepl() is a must


Solution

  • As Frank and others have already pointed out: you need parentheses so your negation is negating the whole argument not just the first part.

    Furthermore if you must use grepl() then fixed = TRUE will increase performance if you are not using any regex:

    DD[!(grepl("BB", RR, fixed = TRUE) & grepl("XLKG", B, fixed = TRUE) & grepl("GA", FF, fixed = TRUE))]
    
       RR    B FF
    1: AA  XLJ KA
    2: BB XLKG CA
    3: BB XLKG FA
    4: BB XLCC GA