approx (x, y = NULL, xout, method = "linear", n = 50,
yleft, yright, rule = 1, f = 0, ties = mean)approxfun(x, y = NULL, method = "linear",
yleft, yright, rule = 1, f = 0, ties = mean)
xy.coords
."linear"
or "constant"
.xout
is not specified, interpolation takes place at
n
equally spaced points spanning the interval [min(x)
,
max(x)
].x
values are
less than min(x)
. The default is defined by the value
of rule
given below.x
values are
greater than max(x)
. The default is defined by the value
of rule
given below.min(x)
, max(x)
].
If rule
is 1
then NA
s are returned for such
points and if it is 2
, the value at the closest data extreme
is used. Use, e.g., rule = 2:1
, if the left and right side
extrapolation should differ.method = "constant"
a number between 0 and 1
inclusive, indicating a compromise between left- and
right-continuous step functions. If y0
and y1
are
the values to the left and right of the point then the value is
y0
if f == 0
, y1
if f == 1
, and
y0*(1-f)+y1*f
for intermediate values. In this way the result is
right-continuous for f == 0
and left-continuous for f
== 1
, even for non-finite y
values.x
values. Either a function
with a single vector argument returning a single number result or
the string "ordered"
.approx
returns a list with components x
and y
,
containing n
coordinates which interpolate the given data
points according to the method
(and rule
) desired. The function approxfun
returns a function performing (linear or
constant) interpolation of the given data points. For a given set of
x
values, this function will return the corresponding
interpolated values. It uses data stored in its environment when it
was created, the details of which are subject to change.approxfun
contains references to the code
in the current version of R: it is not intended to be saved and
loaded into a different R session. This is safer for R >= 3.0.0.(x, y)
pairs are required (for method =
"linear"
, one otherwise). If there are duplicated (tied) x
values and ties
is a function it is applied to the y
values for each distinct x
value.
Useful functions in this context include mean
,
min
, and max
. If ties = "ordered"
the x
values are assumed to be already ordered. The first
y
value will be used for interpolation to the left and the last
one for interpolation to the right.spline
and splinefun
for spline
interpolation.require(graphics)
x <- 1:10
y <- rnorm(10)
par(mfrow = c(2,1))
plot(x, y, main = "approx(.) and approxfun(.)")
points(approx(x, y), col = 2, pch = "*")
points(approx(x, y, method = "constant"), col = 4, pch = "*")
f <- approxfun(x, y)
curve(f(x), 0, 11, col = "green2")
points(x, y)
is.function(fc <- approxfun(x, y, method = "const")) # TRUE
curve(fc(x), 0, 10, col = "darkblue", add = TRUE)
## different extrapolation on left and right side :
plot(approxfun(x, y, rule = 2:1), 0, 11,
col = "tomato", add = TRUE, lty = 3, lwd = 2)
## Show treatment of 'ties' :
x <- c(2,2:4,4,4,5,5,7,7,7)
y <- c(1:6, 5:4, 3:1)
approx(x, y, xout = x)$y # warning
(ay <- approx(x, y, xout = x, ties = "ordered")$y)
stopifnot(ay == c(2,2,3,6,6,6,4,4,1,1,1))
approx(x, y, xout = x, ties = min)$y
approx(x, y, xout = x, ties = max)$y
<!-- %%-- MM has nice utility plotting -- do in demo ? -->
Run the code above in your browser using DataLab