R中的推算后均值 [英] mean-before-after imputation in R

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

问题描述

我是R语言的新手.我的问题是如何使用缺失数据点前后的均值来估算缺失值?

I'm new in R. My question is how to impute missing value using mean of before and after of the missing data point?

示例;

使用每个NA上下的平均值作为推算值.

using the mean from the upper and lower of each NA as the impute value.

-第3行的平均值为38.5

-mean for row number 3 is 38.5

-第7行的平均值是32.5

-mean for row number 7 is 32.5

age
52.0
27.0
NA
23.0
39.0
32.0
NA
33.0
43.0

谢谢.

推荐答案

此处是使用zoo包中na.locf中的from解决方案的解决方案,该解决方案将每个NA替换为之前或之后的最新非NA.

Here a solution using from na.locf from zoo package which replaces each NA with the most recent non-NA prior or posterior to it.

0.5*(na.locf(x,fromlast=TRUE) + na.locf(x))
[1] 52.0 27.0 25.0 23.0 39.0 32.0 32.5 33.0 43.0

如果连续的NA不止一个,这里的优势.

the advantage here if you have more than one consecutive NA.

x <- c(52, 27, NA, 23, 39, NA, NA, 33, 43)
0.5*(na.locf(x,fromlast=TRUE) + na.locf(x))
[1] 52 27 25 23 39 36 36 33 43

编辑 不推荐使用rev自变量,因此我将其替换为fromlast

EDIT rev argument is deprecated so I replace it by fromlast

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

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