如何在R中用行均值估算缺失值 [英] How to impute missing values with row mean in R

查看:222
本文介绍了如何在R中用行均值估算缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个大数据框中提取了一行数字数据,并保存为矢量.一些值丢失并标记为NA.我想用行均值来估算缺失的值.

From a large data frame, I have extracted a row of numeric data and saved as a vector. Some of the values are missing and marked as NA. I want to impute the missing values with row mean.

谢谢

推荐答案

x作为您的载体:

x <- c(NA,0,2,0,2,NA,NA,NA,0,2)
ifelse(is.na(x), mean(x, na.rm = TRUE), x)
# [1] 1 0 2 0 2 1 1 1 0 2

或者,如果您不关心原始矢量,则可以直接对其进行修改:

Or if you don't care for the original vector, you can modify it directly:

x[is.na(x)] <- mean(x, na.rm = TRUE)

这篇关于如何在R中用行均值估算缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆