Linear Interpolation in R-approx

Linear Interpolation in R, You will discover how to use the approx and approxfun interpolation functions in this R tutorial.

Two examples of how to use the approx and approxfun functions for interpolation are provided on this page.

Let’s get started:

Top Data Science Applications You Should Know 2023 (datasciencetut.com)

Example 1: Use two coordinates and the approximate function

The approx function can be used to return a list of points that linearly interpolates given two separate data points in the manner shown in this example.

We must first produce two numerical vectors to represent our data points:

x1 <- c(0, 5)                       
x1                                  
[1] 0 5
y1 <- c(0, 10)                      
y1                                  
[1]  0 10

You can see that we have constructed two vector objects, each of which contains two integers, depending on the previous outputs of the RStudio console.

We can use the approx to our two data coordinates in the following step:

Applications of Data Science in Education – Data Science Tutorials

data_approx1 <- approx(x1, y1)      
data_approx1
$x
[1] 0.0000000 0.1020408 0.2040816 0.3061224 0.4081633 0.5102041 0.6122449
[8] 0.7142857 0.8163265 0.9183673 1.0204082 1.1224490 1.2244898 1.3265306
[15] 1.4285714 1.5306122 1.6326531 1.7346939 1.8367347 1.9387755 2.0408163
[22] 2.1428571 2.2448980 2.3469388 2.4489796 2.5510204 2.6530612 2.7551020
[29] 2.8571429 2.9591837 3.0612245 3.1632653 3.2653061 3.3673469 3.4693878
[36] 3.5714286 3.6734694 3.7755102 3.8775510 3.9795918 4.0816327 4.1836735
[43] 4.2857143 4.3877551 4.4897959 4.5918367 4.6938776 4.7959184 4.8979592
[50] 5.0000000
$y
[1]  0.0000000  0.2040816  0.4081633  0.6122449  0.8163265  1.0204082
[7]  1.2244898  1.4285714  1.6326531  1.8367347  2.0408163  2.2448980
[13]  2.4489796  2.6530612  2.8571429  3.0612245  3.2653061  3.4693878
[19]  3.6734694  3.8775510  4.0816327  4.2857143  4.4897959  4.6938776
[25]  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
[37]  7.3469388  7.5510204  7.7551020  7.9591837  8.1632653  8.3673469
[43]  8.5714286  8.7755102  8.9795918  9.1836735  9.3877551  9.5918367
[49]  9.7959184 10.0000000

As you can see, the approx function gave a list with two list members as its output.

Top Data Science Skills- step by step guide (datasciencetut.com)

Let’s display the following data:

plot(data_approx1$x, data_approx1$y)
points(x1, y1, col = "red", pch = 16)

The output of the previous R syntax is shown in Figure 1; it is a scatterplot of the linear interpolations produced by the approx function.

The interpolation locations are shown in black, while the two original coordinates are indicated in red.

Example 2: Apply the approximate function to numerous coordinates

How to interpolate between two various data points has been demonstrated in Example 1.

Will go through interpolation between various data points in this part.

Before moving forward, we must create two new example vectors:

x2 <- c(0, 5, 10, 15)               
x2                                  
[1]  0  5 10 15
y2 <- c(0, 10, 100, 1000)           
y2                                  
[1]    0   10  100 1000

Each of our vectors has four integer elements, as you can see.

The approx function can then be used, as we did in Example 1:

data_approx2 <- approx(x2, y2)       
data_approx2                        
$x
 [1]  0.0000000  0.3061224  0.6122449  0.9183673  1.2244898  1.5306122
  [7]  1.8367347  2.1428571  2.4489796  2.7551020  3.0612245  3.3673469
 [13]  3.6734694  3.9795918  4.2857143  4.5918367  4.8979592  5.2040816
 [19]  5.5102041  5.8163265  6.1224490  6.4285714  6.7346939  7.0408163
 [25]  7.3469388  7.6530612  7.9591837  8.2653061  8.5714286  8.8775510
 [31]  9.1836735  9.4897959  9.7959184 10.1020408 10.4081633 10.7142857
 [37] 11.0204082 11.3265306 11.6326531 11.9387755 12.2448980 12.5510204
 [43] 12.8571429 13.1632653 13.4693878 13.7755102 14.0816327 14.3877551
 [49] 14.6938776 15.0000000
 $y
  [1]    0.0000000    0.6122449    1.2244898    1.8367347    2.4489796
  [6]    3.0612245    3.6734694    4.2857143    4.8979592    5.5102041
 [11]    6.1224490    6.7346939    7.3469388    7.9591837    8.5714286
 [16]    9.1836735    9.7959184   13.6734694   19.1836735   24.6938776
 [21]   30.2040816   35.7142857   41.2244898   46.7346939   52.2448980
 [26]   57.7551020   63.2653061   68.7755102   74.2857143   79.7959184
 [31]   85.3061224   90.8163265   96.3265306  118.3673469  173.4693878
 [36]  228.5714286  283.6734694  338.7755102  393.8775510  448.9795918
 [41]  504.0816327  559.1836735  614.2857143  669.3877551  724.4897959
 [46]  779.5918367  834.6938776  889.7959184  944.8979592 1000.0000000

These data should be plotted as a scatterplot:

plot(data_approx2$x,              
 data_approx2$y)
points(x2, y2,
       col = "red",
       pch = 16)

The scatterplot in Figure 2 has been created after the previous R programming syntax has been run. As you can see, we interpolated linearly between several data points.

You may also like...

Leave a Reply

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

16 + twenty =