Or like this:
x <- matrix(sample(c(NA,1:10),100,TRUE),nrow=10)
x
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 7 7 1 6 7 3 10 4 NA 2
[2,] 3 2 7 9 1 4 2 5 10 1
[3,] 10 4 2 8 7 4 1 8 8 3
[4,] 7 7 6 9 2 6 NA 6 6 10
[5,] 1 NA 5 9 9 4 NA 5 8 2
[6,] 4 4 9 3 9 4 5 NA 5 1
[7,] NA 2 2 2 9 2 10 NA 8 7
[8,] 10 8 7 1 5 2 9 7 10 5
[9,] 6 3 10 9 8 6 7 10 3 10
[10,] 7 9 5 2 2 9 5 6 NA 9
means <- colMeans(x,na.rm=TRUE)
for(i in 1:ncol(x)){
x[is.na(x[,i]),i] <- means[i]
}
x
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 7.000000 7.000000 1 6 7 3 10.000 4.000 7.25 2
[2,] 3.000000 2.000000 7 9 1 4 2.000 5.000 10.00 1
[3,] 10.000000 4.000000 2 8 7 4 1.000 8.000 8.00 3
[4,] 7.000000 7.000000 6 9 2 6 6.125 6.000 6.00 10
[5,] 1.000000 5.111111 5 9 9 4 6.125 5.000 8.00 2
[6,] 4.000000 4.000000 9 3 9 4 5.000 6.375 5.00 1
[7,] 6.111111 2.000000 2 2 9 2 10.000 6.375 8.00 7
[8,] 10.000000 8.000000 7 1 5 2 9.000 7.000 10.00 5
[9,] 6.000000 3.000000 10 9 8 6 7.000 10.000 3.00 10
[10,] 7.000000 9.000000 5 2 2 9 5.000 6.000 7.25 9
This is not quite exactly what you are looking for but might be useful. This function substitute all NA with median (in every column):
require(randomForest)
x <- matrix(sample(c(NA,1:10),100,TRUE),nrow=10)
na.roughfix(x)