Imputation with column medians in R

2019-09-14 23:16发布

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.

标签: r matrix apply
2条回答
爷的心禁止访问
2楼-- · 2019-09-14 23:53

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
查看更多
趁早两清
3楼-- · 2019-09-15 00:04

There is a convenient function (na.aggregate) in zoo to replace the NA elements with the specified FUN.

library(zoo)
apply(mat, 2, FUN = function(x) na.aggregate(x, FUN = median))
#      [,1] [,2] [,3]
#[1,]    1    6    4
#[2,]    3    7    4
#[3,]    3    6    2
#[4,]    5    3    8

Or as @G.Grothendieck commented, na.aggregate can be directly applied on the matrix

na.aggregate(mat, FUN = median)
查看更多
登录 后发表回答