R 中列中位数的插补 [英] Imputation with column medians in R
问题描述
如果我有一个向量,例如
If I have a vector, for example
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.
推荐答案
在 apply 函数的最后一行添加 return(x)
即可解决.
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屋!