内插NA值 [英] Interpolate NA values

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

问题描述

我有两组与时间无关的样本.我想将它们合并并计算缺失值 在我没有两者兼具的时代.简化示例:

I have two set of samples that are time independent. I would like to merge them and calculate the missing values for the times where I do not have values of both. Simplified example:

A <- cbind(time=c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100),
           Avalue=c(1, 2, 3, 2, 1, 2, 3, 2, 1, 2))
B <- cbind(time=c(15, 30, 45, 60), Bvalue=c(100, 200, 300, 400))
C <- merge(A,B, all=TRUE)

   time Avalue Bvalue
1    10      1     NA
2    15     NA    100
3    20      2     NA
4    30      3    200
5    40      2     NA
6    45     NA    300
7    50      1     NA
8    60      2    400
9    70      3     NA
10   80      2     NA
11   90      1     NA
12  100      2     NA

通过假设每个样本之间的线性变化,可以计算缺失的NA值. 从直觉上很容易看出,在时间15和45处的A值应为1.5.但是对B的正确计算 例如在20点会是

By assuming linear change between each sample, it is possible to calculate the missing NA values. Intuitively it is easy to see that the A value at time 15 and 45 should be 1.5. But a proper calculation for B for instance at time 20 would be

100 +(20-15)*(200-100)/(30-15)

100 + (20 - 15) * (200 - 100) / (30 - 15)

等于133.33333. 第一个括号是估计时间与最后一个可用样本之间的时间. 第二个括号是最接近的样本之间的差. 第三个括号是最接近的样本之间的时间.

which equals 133.33333. The first parenthesis being the time between estimate time and the last sample available. The second parenthesis being the difference between the nearest samples. The third parenthesis being the time between the nearest samples.

如何使用R计算NA值?

How can I use R to calculate the NA values?

推荐答案

使用zoo软件包:

library(zoo)
Cz <- zoo(C)
index(Cz) <- Cz[,1]
Cz_approx <- na.approx(Cz)

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

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