R中的近似查找 [英] Approximate lookup in R

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

问题描述

我有以下查找表:

lkp <- data.frame(
         x=c(0,0.2,0.65,0.658,1.3,1.76,2.7), 
         y=c(1,1,1,0.942,0.942, 0.92, 0.89)
       )

我想获得给定X值的Y值.

I would like to get the value of Y of a given X value.

如果表中存在X值,则应返回表的确切Y. 如果X值不存在,则应将Y值作为2个最近邻居(仅2个最近邻居)的线性插值返回.我不想让模型适合整体数据.

If the X value exists in the table then the exact Y of the table should be returned. If the X value does not exist, then the Y value should be returned as linear interpolation of the 2 nearest neighbors (only the 2 nearest neighbors). I would not like to fit a model to the overall data.

上表

for X=0.2 Y=1 (exact lookup) 
for X=2 Y=0.91 (linear interpolation between the last 2 rows of the data frame)

有准备好执行此操作的功能吗?

Is there any ready function to do this?

推荐答案

是的,它称为approx.

> with(lkp, approx(x, y, xout=c(0.2, 2)))
$x
[1] 0.2 2.0

$y
[1] 1.0000000 0.9123404

有关更多信息,请参见?approx.

See ?approx for more information.

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

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