Delete rows containing specific strings in R

user3091668 picture user3091668 · Mar 7, 2014 · Viewed 138.9k times · Source

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

Answer

Pop picture Pop · Mar 7, 2014

This should do the trick:

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

Or a safer version would be:

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