0

EDIT: tried grepl, as in:

DF <- DF[!grepl("M+1", DF$Column2, fixed=TRUE),]

(the fixed=TRUE is necessary because of the "+" in the string).

Original post:

My data frame contains a particular column that has a wide range of factors, and I want to delete any row in which that factor contains a particular string.

For example, I want to delete rows 1 and 3 from the table below, because they contain the conserved string "M+1".

Column1    Column2    Column3
x          [3]M+1     y
x                     y
x          [2]M+1     y
x                     y

I'd be fine if the column entry was just a static string, e.g.

DF <- DF[!(DF$Column2 == "M+1"),]

But unfortunately there are other, irrelevant components to the factors in the column. I considered using %in% but can't figure out how to make it "not in" rather than "in". I've tried here, here, here and here etc. but they're mainly dealing with deleting rows based on numeric values rather than factors (and based on the absolute value in the column, rather than whether it "contains" a particular value). Thanks.

heds1
  • 3,203
  • 2
  • 17
  • 32
  • Check out `?grepl` as in `grepl("M+1", DF$Column2, fixed=TRUE)` – thelatemail Oct 26 '17 at 03:51
  • Thanks @thelatemail, that looks promising, but for some reason it's not working for the "M+1" string (while it works for the "REVERSE" string as in the example you linked as duplicate). – heds1 Oct 26 '17 at 04:16
  • Because `+` is a special character in regular expressions. Notice my comment above has `fixed=TRUE` - either use that or escape the `+` sign like `"M\\+1"` – thelatemail Oct 26 '17 at 04:19
  • I thought I'd tried that straight away but must have missed it. Thank you very much. – heds1 Oct 26 '17 at 04:27

0 Answers0