[R] Exchange NAs for mean

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Dec 17 15:41:38 CET 2009


2009/12/17 Joel Fürstenberg-Hägg <joel_furstenberg_hagg at hotmail.com>:
>
> Hi all,
>
>
>
> I'm have a matrix (X) with observations as rows and parameters as columns. I'm trying to exchange all missing values in a column by the column mean using the code below, but so far, nothing happens with the NAs... Can anyone see where the problem is?
>
>
>
> N<-nrow(X) # Calculate number of rows = 108
> p<-ncol(X) # Calculate number of columns = 88
>
>
> # Replace by columnwise mean
> for (i in colnames(X)) # Do for all columns in the matrix
> {
>   for (j in rownames(X)) # Go through all rows
>   {
>      if(is.na(X[j,i])) # Search for missing value in the given position
>      {
>         X[j,i]=mean(X[1:p, i]) # Change missing value to the mean of the column
>      }
>   }
> }
>


 mean(anything with an NA in it) == NA. You want mean(X[1:p,i],na.rm=TRUE)

 > mean(c(1,2,3,NA,4))
 [1] NA
 > mean(c(1,2,3,NA,4),na.rm=TRUE)
 [1] 2.5

I'll leave it to someone else to show you how to speed this code up by
removing the loops...

Barry




More information about the R-help mailing list