[R] change specific factor level values to NA in data frame
Daniel Caro
dcarov at gmail.com
Mon Sep 30 11:42:04 CEST 2013
Dear R-users
I am trying to replace specific factor level values in a data frame
with NAs. The data frame includes different kind of variables (e.g,
characters, numbers, and factors). I'd like to replace all 'Not
applicable', 'Invalid', 'and Missing' for NA.
For example:
f.level <- c('Yes', 'No', 'Not applicable', 'Invalid', 'Missing')
df <- data.frame(x1=runif(100), x2=sample(f.level, 100, replace=T),
x3=sample(f.level, 100, replace=T))
I try changing the values by
df[df %in% c('Not applicable', 'Invalid', 'Missing'), ] <- NA
but nothing seems to change
summary(df)
My data frame has many more factors. Any advice?
Thank you,
Daniel
More information about the R-help
mailing list