Search code examples
rsubsetgrepl

R - subset - exclude rows based on grepl selection of column value


I am successfully able to select the items that I don't want.

df3 <- subset(df2, grepl("^Imag.*", appt_type))

What I am trying to do is create the inverse of that previous command. I want to create a new dataset(df3) that consists of all the rows from df2 except if the appt_type value begins with "Imag". I have tried many combinations of [ ] and ! in different places but can't seem to get this right. Been combing the message boards and know I am missing something simple.

Any help is much appreciated.


Solution

  • df3 <- subset(df2, !grepl("^Imag.*", appt_type))
    

    Basically find all NOT (!) starting with Imag.