Home > Blockchain >  What is the function of $y at the end of an interpolation function?
What is the function of $y at the end of an interpolation function?

Time:02-04

I am learning interpolation using R. I found a code online and I am not sure about the function of $y at the end of the code. I tried to run the code with and without $y and I cannot really tell the differences.

test <- approx(x, y, xout=c, rule=2) $y

CodePudding user response:

$ is one way to access elements of a list in R.

(Some more explanation: https://stackoverflow.com/a/1169495/6851825 or https://stackoverflow.com/a/32819641/6851825)

This code will produce a list of x values (from the c term we provided) and interpolated y values.

x = 1:3
y = 4:6
c = seq(1,3,by = 0.5)
approx (x, y, xout=c, rule=2)

The output R gives us tells us that it's a list with components called x and y.

$x
[1] 1.0 1.5 2.0 2.5 3.0

$y
[1] 4.0 4.5 5.0 5.5 6.0

If we just want the y part, the interpolated values, we can run approx (x, y, xout=c, rule=2)$y to get it. Instead of a list, this is a numeric vector.

[1] 4.0 4.5 5.0 5.5 6.0
  •  Tags:  
  • Related