找到并替换行平均值的缺失值 [英] Find and replace missing values with row mean
本文介绍了找到并替换行平均值的缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
c1 = c(1, 2,3,NA)
c2 = c(3,1,NA,3)
c3 = c(2,1,3,1)
df = data。框架(c1,c2,c3)
> df
c1 c2 c3
1 1 3 2
2 2 1 1
3 3 NA 3
4 NA 3 1
,以便
> df
c1 c2 c3
1 1 3 2
2 2 1 1
3 3 3 3
4 2 3 1
解决方案
非常类似于@ baptiste的答案
> ind < - which(is.na(df),arr.ind = TRUE)
> df [ind]< - rowMeans(df,na.rm = TRUE)[ind [,1]]
I have a data frame with NAs and I want to replace the NAs with row means
c1 = c(1,2,3,NA)
c2 = c(3,1,NA,3)
c3 = c(2,1,3,1)
df = data.frame(c1,c2,c3)
> df
c1 c2 c3
1 1 3 2
2 2 1 1
3 3 NA 3
4 NA 3 1
so that
> df
c1 c2 c3
1 1 3 2
2 2 1 1
3 3 3 3
4 2 3 1
解决方案
Very similar to @baptiste's answer
> ind <- which(is.na(df), arr.ind=TRUE)
> df[ind] <- rowMeans(df, na.rm = TRUE)[ind[,1]]
这篇关于找到并替换行平均值的缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文