Joe - 8 months ago 41

R Question

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(is.na(vec))] <- 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(is.na(x))] <- 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

Adding `return(x)`

as last line of the function within apply will solve it.

```
> apply(mat, 2, function(x){
x[which(is.na(x))] <- median(x, na.rm=T)
return(x)
})
[,1] [,2] [,3]
[1,] 1 6 4
[2,] 3 7 4
[3,] 3 6 2
[4,] 5 3 8
```

Source (Stackoverflow)