Apply approx Function to Two Coordinates

In this method, we will be applying the approx function to two coordinates using the approx function passed with the given vector as its parameters to get a list of points which interpolate given two different data points.

Example:

In this example, we have used two vectors, first from 0 to 10 and another from 0 to 20, and further with the call of the approx function and passing the created vector to the function we get the list of points that linearly interpolate n and further form better visualization we have to create the plot the points in the R programming language.

R




# Create vector
x <- c(0, 10)    
y <- c(0, 20)  
  
# Apply approx function
data_approx1 <- approx(x, y)       
data_approx1    
  
# Draw output of approx function
plot(data_approx1$x,                 
     data_approx1$y)
points(x, y,
       col = "red",
       pch = 16)


Output:

$x

 [1]  0.0000000  0.2040816  0.4081633  0.6122449  0.8163265  1.0204082  1.2244898  1.4285714  1.6326531  1.8367347

[11]  2.0408163  2.2448980  2.4489796  2.6530612  2.8571429  3.0612245  3.2653061  3.4693878  3.6734694  3.8775510

[21]  4.0816327  4.2857143  4.4897959  4.6938776  4.8979592  5.1020408  5.3061224  5.5102041  5.7142857  5.9183673

[31]  6.1224490  6.3265306  6.5306122  6.7346939  6.9387755  7.1428571  7.3469388  7.5510204  7.7551020  7.9591837

[41]  8.1632653  8.3673469  8.5714286  8.7755102  8.9795918  9.1836735  9.3877551  9.5918367  9.7959184 10.0000000

$y

 [1]  0.0000000  0.4081633  0.8163265  1.2244898  1.6326531  2.0408163  2.4489796  2.8571429  3.2653061  3.6734694

[11]  4.0816327  4.4897959  4.8979592  5.3061224  5.7142857  6.1224490  6.5306122  6.9387755  7.3469388  7.7551020

[21]  8.1632653  8.5714286  8.9795918  9.3877551  9.7959184 10.2040816 10.6122449 11.0204082 11.4285714 11.8367347

[31] 12.2448980 12.6530612 13.0612245 13.4693878 13.8775510 14.2857143 14.6938776 15.1020408 15.5102041 15.9183673

[41] 16.3265306 16.7346939 17.1428571 17.5510204 17.9591837 18.3673469 18.7755102 19.1836735 19.5918367 20.0000000

 

Interpolation Functions in R

In this article, we will be looking towards the approx() and the aproxfun() interpolation function with working examples in the R Programming language.

Similar Reads

Approx() and Approxfun() interpolation function

These functions return a list of points that linearly interpolates given data points, or a function performing the linear (or constant) interpolation....

Method 1: Apply approx Function to Two Coordinates

In this method, we will be applying the approx function to two coordinates using the approx function passed with the given vector as its parameters to get a list of points which interpolate given two different data points....

Method 2: Apply approx Function to Multiple Coordinates

...

Method 3: Create a User-Defined Interpolation Function Using approxfun

In this method to apply an approx function to multiple coordinates, the user has to create the vectors containing multiple coordinates which further have to be passed as done in the above approach function which will be returning the linearly interpolates a list of the vector passed to the user....