Jim Maas
Jim Maas

Reputation: 1729

search and replace strings, not matching pattern, and NA in R

I have character vector (column) in a data.frame that contains three correct values comprised of "Positive", "Negative" and NA, along a small number of incorrect values. I want to replace the incorrect values, i.e. everything except "Positive", "Negative" and "NA" with "Positive" I can grep for NOT "Positive" or "Negative", using grep and the invert=TRUE argument, and get everything except instances of "Negative" and "Positive" but I've not figured out how to additionally, not replace any instances of NA. I've also looked at the sub command, but it does not have an invert option. Any suggestions?

Upvotes: 0

Views: 1162

Answers (1)

Jaap
Jaap

Reputation: 83215

This should work:

df[!is.na(df$column) & !df$column %in% 
        c("Positive","Negative"), "column"] <- "Positive"

Upvotes: 3

Related Questions