Ys Kee Ys Kee - 1 month ago 8
R Question

How to find points by a linear interpolation in R

I have two points (5,0.45) & (6,0.50) and need to find the value when x=5.019802 by linear interpolation

But how to code it in R?

I got code below but just got a graph insdeed.

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')

Answer

You just need to specify an xout value.

approx(x,y,xout=5.019802)
$x
[1] 5.019802

$y
[1] 0.4509901
Comments