How to Use approxfun() Function in R


Often you may want to find a list of points that linearly interpolate a set of given data points in R.

The easiest way to do so is by using the approxfun() function from base R, which is designed to perform this exact task.

The approxfun() function uses the following basic syntax:

approxfun(x, y=NULL, method=”linear”, …)

where:

  • x: Numeric vector of x-coordinates to be linearly interpolated
  • y: Numeric vector of y-coordinates to be linearly interpolated
  • method: The interpolation method to use (choices are “linear” or “constant”)

The following example shows how to use the approxfun() function in practice in several different scenarios.

Note: The approxfun() function comes built-in with base R so you do not need to install or load any external packages to use this function.

Example: How to Use the approxfun() Function in R

Suppose that we define two vectors, x and y:

#create x and y vectors
x <- c(1, 2, 3, 4, 5)
y <- c(1, 4, 9, 15, 22)

We can use the plot() function from base R to create a scatterplot of the points in these two vectors:

#create scatterplot of x vs. y
plot(x, y, col='blue', pch=19))

This produces the following scatterplot:

Note: We used the col argument to specify the color of the points in the plot and we used the pch argument with a value of 19 to specify that the points should be filled-in circles in the plot.

Now suppose that we would like to perform linear interpolation to create a list of points that interpolate the set of points provided by the x and y vectors.

We can use the approxfun() function to determine the function that can interpolate these points and then use the curve() function to create a curve that displays the results of the approxfun() function overlaid on the original set of (x, y) coordinates:

#perform linear interpolation using provided x and y vectors
f <- approxfun(x, y)

#plot curve of interpolated points
curve(f(x), 1, 5, col ='red')

#add original (x, y) points to plot
points(x, y, col='blue', pch=19)

This produces the following chart:

approxfun() function in R example

The blue points represent the original (x, y) coordinates and the blue line represents the linearly interpolated curve.

Note that in this example we used the approxfun() function to simply find the function that could be used to linearly interpolate the original set of (x, y) points.

We then used the plot() function to create a plot to visualize the results of the linear interpolation.

From the plot we can see that the red curve seems to linearly interpolate the points perfectly.

Note that in this example we used two vectors with only five points each but you can use the approxfun() function to linearly interpolate points for vectors of any size. The only requirement is that the two vectors have the same number of elements.

Additional Resources

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

How to Use str_split in R
How to Use str_replace in R
How to Count Words in String in R
How to Convert a Vector to String in R

Leave a Reply

Your email address will not be published. Required fields are marked *