【发布时间】:2016-10-18 23:00:30
【问题描述】:
我有两个点 (5,0.45) & (6,0.50),需要通过线性插值求 x=5.019802 时的值
但是如何在 R 中编码呢?
我尝试了下面的代码,但得到了一个图表。
x <- c(5,6)
y <- c(0.45,0.50)
interp <- approx(x,y)
plot(x,y,pch=16,cex=2)
points(interp,col='red')
【问题讨论】:
-
精确值 5.019802,不会出现在
interp$x中。您可以尝试使用targetVal = 5.019802 ; which.min(abs(interp$x - targetVal ))将索引设为 2 来找到最接近目标值的点,interp$x[2],interp$y[2]将是最接近所需值的点