[R] handling NA by mean replacement

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jan 30 18:26:10 CET 2006


Don't know about efficiency but here is one way:

# test data
A  <- matrix(1:54, ncol=6)
A[3,3] <- A[6,6] <- A[5,6] <- NA

f <- function(x) ifelse(is.na(x), mean(x, na.rm = TRUE), x)
apply(A, 2, f)

On 1/30/06, Julie Bernauer <julie.bernauer at ibbmc.u-psud.fr> wrote:
> Hello
>
> I am sorry fuch such a stupid question. Suppose I have a table of data having a
> lot of NAs and I want to replace those NAs by the mean of the column before NA
> replacement. How is it possible to do that efficiently ?
>
> Thanks in advance,
>
> Julie
>
> --
> Julie Bernauer
> Yeast Structural Genomics
> http://www.genomics.eu.org
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list