用R中的列中位数进行插补 [英] Imputation with column medians in R
问题描述
例如,如果我有矢量
vec <- c(3,4,5,NA)
我可以使用以下代码将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)
但是,如果我有一个包含NA的矩阵,则在矩阵的所有列上应用相同的代码并不会给我返回矩阵,而只是返回每个矩阵列的中位数.
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
如何用NA中位数代替列中位数来取回矩阵?这个问题类似:按行均值替换NA值,但我不能根据我的情况调整任何解决方案.
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.
推荐答案
将return(x)
添加为apply中函数的最后一行即可解决该问题.
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
这篇关于用R中的列中位数进行插补的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!