Hi Patrik,
If speed is an issue, an easy way to go a bit faster using Dr.
Spector's code is:
wh = which(is.na(mat), arr.ind=TRUE)
mat[wh] = colMeans(mat, na.rm=TRUE)[wh[,2]]
using that method, I can replace the NA values with column means on a
3 x 1 matrix with ~ 1% of values missing in
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
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]]
__
3 matches
Mail list logo