R缺失值替换功能 [英] R Missing Value Replacement Function

查看:121
本文介绍了R缺失值替换功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个缺少值的表,我正在尝试编写一个函数来替换 缺失值,并基于最接近的两个非零值进行计算.

I have a table with missing values and I'm trying to write a function that will replace the missing values with a calculation based on the nearest two non-zero values.

示例:

X  Tom    
1  4.3    
2  5.1    
3  NA    
4  NA    
5  7.4

对于X = 3Tom = 5.1 + (7.4-5.1)/2.

对于X = 4Tom = (5.1 + (7.4-5.1)/2) + (7.4-5.1)/2

此功能已经存在吗?如果没有,将不胜感激任何建议.

Does this function already exist? If not, any advice would be greatly appreciated.

推荐答案

更常见的方法(但不等于问题)是使用线性插值:

A more usual way to do this (but not equivalent to the question) is to use linear interpolation:

library(zoo)
df <- data.frame(X = 1:5, Tom = c(4.3, 5.1, NA, NA, 7.4))

na.approx(df)

或样条插值:

na.spline(df)

这篇关于R缺失值替换功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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