[R] NA matrix operation

Phil Spector spector at stat.berkeley.edu
Wed Dec 15 23:17:03 CET 2010


Patrik -
     I don't know if it's the fastest, but, assuming your 
Matrix is called mat, this seems to work fairly quickly:

wh =  which(is.na(mat),arr.ind=TRUE)
mat[wh] = apply(mat,2,mean,na.rm=TRUE)[wh[,2]]

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


On Wed, 15 Dec 2010, patrik.waldmann at djingis.se wrote:

>
>
> 	Dear All,
>
> 	??
>
> 	does anyone know which is the fastest way to replace NA values in a
> Matrix by their column mean?
>
> 	library(Matrix)
> mat
>
> Links:
> ------
> [1] http://www.ownit.se
>
>
> 	[[alternative HTML version deleted]]
>
>



More information about the R-help mailing list