Search code examples
rstringmatchrows

Delete rows containing specific strings in R


I would like to exclude lines containing a string "REVERSE", but my lines do not match exactly with the word, just contain it.

My input data frame:

   Value   Name 
    55     REVERSE223   
    22     GENJJS
    33     REVERSE456
    44     GENJKI

My expected output:

   Value   Name 
    22     GENJJS
    44     GENJKI

Solution

  • This should do the trick:

    df[- grep("REVERSE", df$Name),]
    

    Or a safer version would be:

    df[!grepl("REVERSE", df$Name),]