本文介绍了用周围环境值的平均值替换缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据集如下所示(我们称之为a"):

My dataset looks like the following (let's call it "a"):

date value
2013-01-01 12.2
2013-01-02 NA
2013-01-03 NA
2013-01-04 16.8
2013-01-05 10.1
2013-01-06 NA
2013-01-07 12.0

我想用最近的环境值(系列中的前一个和下一个值)的平均值替换 NA.

I would like to replace the NA by the mean of the closest surroundings values (the previous and the next values in the series).

我尝试了以下操作,但我不相信输出...

I tried the following but I am not convinced by the output...

miss.val = which(is.na(a$value))
library(zoo)
z = zoo(a$value, a$date)
z.corr = na.approx(z)
z.corr[(miss.val - 1):(miss.val + 1), ]

推荐答案

Using na.locf (Last Observation Carried Forward) from package zoo:

Using na.locf (Last Observation Carried Forward) from package zoo:

R> library("zoo")
R> x <- c(12.2, NA, NA, 16.8, 10.1, NA, 12.0)
R> (na.locf(x) + rev(na.locf(rev(x))))/2
[1] 12.20 14.50 14.50 16.80 10.10 11.05 12.00

(如果 x 的第一个或最后一个元素是 NA,则不起作用)

(does not work if first or last element of x is NA)

这篇关于用周围环境值的平均值替换缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 13:59