[R] Replacing missing values
mark at myatt.demon.co.uk
Wed May 9 12:20:15 CEST 2001
Jean Vidal <jean.vidal at freesurf.fr> writes:
>I'm discovering R (very impressive), and didn't find in the docs a simple
>method for replacing, in a data frame, missing values (NA) with the
>column's mean (or any other method for reconstructing missing values when
>Thanks in advance for your help.
There are a series of na.* Functions in R but they are not well
documented (which is my way of saying "I can't work out how to make them
I use indexing to deal with missing values. For example:
var[var == -99] <- NA
To replace a missing value code (e.g. -99) with NA. Replacing with an
imputed value can be done in the same manner but with the imputation
function on the RHS of the assignment. For example:
var[is.na(var)] <- mean(var, na.rm = TRUE)
If var is a vector in a data.frame then you need to specify the
df$var[is.na(df$var)] <- mean(df$var, na.rm = TRUE)
I hope that helps.
BTW: While I am here, can anyone explain how the na.* functions work?
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
More information about the R-help