$
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
We could examine the structure of this output by running:
str(approx (x, y, xout=c, rule=2))
#List of 2
# $ x: num [1:5] 1 1.5 2 2.5 3
# $ y: num [1:5] 4 4.5 5 5.5 6
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