Re: [R] Replacing missing values

About this list Date view Thread view Subject view Author view

From: Mark Myatt (mark@myatt.demon.co.uk)
Date: Wed 09 May 2001 - 20:20:15 EST


Message-ID: <O987XNAfnR+6EwTw@myatt.demon.co.uk>

Jean Vidal <jean.vidal@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
>needed).
>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
work"!).

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
data.frame:

        df$var[is.na(df$var)] <- mean(df$var, na.rm = TRUE)

I hope that helps.

Mark

BTW: While I am here, can anyone explain how the na.* functions work?

--
Mark Myatt

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


About this list Date view Thread view Subject view Author view

This archive was generated by hypermail 2b30 : Fri 22 Jun 2001 - 18:58:41 EST