Since PikkuKatja and glallen have asked for a more general solution, and I still can not comment, I will write an answer. You can combine statements as in:
> df[df=="" | df==12] <- NA > df AB 1 <NA> <NA> 2 xyz <NA> 3 jkl 100
For factors, the zxzak code already gives factors:
> df <- data.frame(list(A=c("","xyz","jkl"), B=c(12,"",100))) > str(df) 'data.frame': 3 obs. of 2 variables: $ A: Factor w/ 3 levels "","jkl","xyz": 1 3 2 $ B: Factor w/ 3 levels "","100","12": 3 1 2
If in trouble I suggest temporarily abandoning the factors.
df[] <- lapply(df, as.character)
sedot Dec 08 '15 at 1:12 2015-12-08 01:12
source share