[R] Exchange NAs for mean
Chuck Cleland
ccleland at optonline.net
Thu Dec 17 15:44:38 CET 2009
On 12/17/2009 9:31 AM, Joel Fürstenberg-Hägg wrote:
> 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
> }
> }
> }
> mymat <- matrix(runif(50), ncol=5)
> mymat[c(3,4,9),c(1,2,5)] <- NA
> mymat
[,1] [,2] [,3] [,4] [,5]
[1,] 0.05863667 0.23545794 0.25549983 0.96275422 0.1015316
[2,] 0.66107818 0.15846239 0.05112992 0.09081990 0.6097318
[3,] NA NA 0.86474629 0.73186676 NA
[4,] NA NA 0.26226776 0.31987242 NA
[5,] 0.78472732 0.09072242 0.24557669 0.57100857 0.1568413
[6,] 0.42431343 0.37551338 0.86085073 0.62782597 0.5090823
[7,] 0.44609972 0.90125504 0.52285650 0.41298482 0.3192929
[8,] 0.27676684 0.17200162 0.70648140 0.86983249 0.2035595
[9,] NA NA 0.34956846 0.07070571 NA
[10,] 0.01482263 0.20074897 0.41553916 0.82367719 0.9674044
> apply(mymat, 2, function(x){x <- replace(x, is.na(x), mean(x,
na.rm=TRUE))})
[,1] [,2] [,3] [,4] [,5]
[1,] 0.05863667 0.23545794 0.25549983 0.96275422 0.1015316
[2,] 0.66107818 0.15846239 0.05112992 0.09081990 0.6097318
[3,] 0.38092069 0.30488025 0.86474629 0.73186676 0.4096348
[4,] 0.38092069 0.30488025 0.26226776 0.31987242 0.4096348
[5,] 0.78472732 0.09072242 0.24557669 0.57100857 0.1568413
[6,] 0.42431343 0.37551338 0.86085073 0.62782597 0.5090823
[7,] 0.44609972 0.90125504 0.52285650 0.41298482 0.3192929
[8,] 0.27676684 0.17200162 0.70648140 0.86983249 0.2035595
[9,] 0.38092069 0.30488025 0.34956846 0.07070571 0.4096348
[10,] 0.01482263 0.20074897 0.41553916 0.82367719 0.9674044
> All the best,
>
>
>
> Joel
>
>
>
>
>
>
> _________________________________________________________________
> Hitta hetaste singlarna på MSN Dejting!
> http://dejting.se.msn.com/channel/index.aspx?trackingid=1002952
> [[alternative HTML version deleted]]
>
>
>
> ------------------------------------------------------------------------
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894
More information about the R-help
mailing list