**Linear interpolation** is the process of estimating an unknown value of a function between two known values.

Given two known values (x_{1}, y_{1}) and (x_{2}, y_{2}), we can estimate the y-value for some point x by using the following formula:

y = y_{1} + (x-x_{1})(y_{2}-y_{1})/(x_{2}-x_{1})

The following example shows how perform linear interpolation in R.

**Example: Linear Interpolation in R**

Suppose we have the following data frame with x and y values in R:

#define data frame df <- data.frame(x=c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20), y=c(4, 7, 11, 16, 22, 29, 38, 49, 63, 80)) #view data frame df x y 1 2 4 2 4 7 3 6 11 4 8 16 5 10 22 6 12 29 7 14 38 8 16 49 9 18 63 10 20 80

We can use the following code to create a scatterplot to visualize the (x, y) values in the data frame:

#create scatterplot plot(df$x, df$y, col='blue', pch=19)

Now suppose that we’d like to find the y-value associated with a new x-value of **13**.

We can use the **approx()** function in R to do so:

**#fit linear regression model using data frame
model <- lm(y ~ x, data = df)
#interpolate y value based on x value of 13
y_new = approx(df$x, df$y, xout=13)
#view interpolated y value
y_new
$x
[1] 13
$y
[1] 33.5
**

The estimated y-value turns out to be **33.5**.

If we add the point (13, 33.5) to our plot, it appears to match the function quite well:

#create scatterplot plot(df$x, df$y, col='blue', pch=19) #add the predicted point to the scatterplot points(13, y_new$y, col='red', pch=19)

We can use this exact formula to perform linear interpolation for any new x-value.

**Additional Resources**

The following tutorials explain how to perform other common tasks in R:

How to Solve a System of Equations in R

How to Predict Values in R Using Multiple Regression Model