Replacing character values with NA in a data frame
I have a data frame containing (in random places) a character value (say "foo" ) that I want to replace with a NA . What's the best way to do so across the whole data frame? c-urchin This: df[ df == "foo" ] <- NA One way to nip this in the bud is to convert that character to NA when you read the data in in the first place. df <- read.csv("file.csv", na.strings = c("foo", "bar")) Another option is is.na<- : is.na(df) <- df == "foo" Note that its use may seem a bit counter-intuitive, but it actually assigns NA values to df at the index on the right hand side. This could be done with dplyr: