Joe Joe - 1 year ago 101
R Question

Replacement of matrix NAs with column medians in R

If I have a vector, for example

vec <- c(3,4,5,NA)

I can replace the NA with the median value of the other values in the vector with the following code:

vec[which(] <- median(vec, na.rm = T)

However, if I have a matrix containing NAs, applying this same code across all columns of the matrix doesn't give me back a matrix, just returning the medians of each matrix column.

mat <- matrix(c(1,NA,3,5,6,7,NA,3,4,NA,2,8), ncol = 3)
apply(mat, 2, function(x) x[which(] <- median(x, na.rm=T) )

#[1] 3 6 4

How can I get the matrix back with NAs replaced by column medians? This question is similar: Replace NA values by row means but I can't adapt any of the solutions to my case.

Answer Source

Adding return(x) as last line of the function within apply will solve it.

> apply(mat, 2, function(x){
    x[which(] <- median(x, na.rm=T)
     [,1] [,2] [,3]
[1,]    1    6    4
[2,]    3    7    4
[3,]    3    6    2
[4,]    5    3    8
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download