Table of Contents
Linear interpolation is a method used to estimate values between two known data points. In R, this can be performed by using the “approx” function, which takes in two vectors representing the x and y coordinates of the known data points. The function then calculates the linear interpolation for any given x value within the range of the known data points. An example of this would be:
x
Perform Linear Interpolation in R (With Example)
Linear interpolation is the process of estimating an unknown value of a function between two known values.
Given two known values (x1, y1) and (x2, y2), we can estimate the y-value for some point x by using the following formula:
y = y1 + (x-x1)(y2-y1)/(x2-x1)
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)

Additional Resources
The following tutorials explain how to perform other common tasks in R:
Cite this article
stats writer (2024). How can linear interpolation be performed in R? Can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-linear-interpolation-be-performed-in-r-can-you-provide-an-example/
stats writer. "How can linear interpolation be performed in R? Can you provide an example?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-linear-interpolation-be-performed-in-r-can-you-provide-an-example/.
stats writer. "How can linear interpolation be performed in R? Can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-linear-interpolation-be-performed-in-r-can-you-provide-an-example/.
stats writer (2024) 'How can linear interpolation be performed in R? Can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-linear-interpolation-be-performed-in-r-can-you-provide-an-example/.
[1] stats writer, "How can linear interpolation be performed in R? Can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can linear interpolation be performed in R? Can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
