R中的近似查找

ECI*_*CII 8 lookup 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)
       )
Run Code Online (Sandbox Code Playgroud)

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

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

对于上表

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)
Run Code Online (Sandbox Code Playgroud)

有没有准备好的功能呢?

And*_*rie 10

是的,它被称为approx.

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

$y
[1] 1.0000000 0.9123404
Run Code Online (Sandbox Code Playgroud)

有关?approx更多信息,请参阅