approxfun {stats} | R Documentation |

Return a list of points which linearly interpolate given data points, or a function performing the linear (or constant) interpolation.

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)

`x, y` |
numeric vectors giving the coordinates of the points to be
interpolated. Alternatively a single plotting structure can be
specified: see |

`xout` |
an optional set of numeric values specifying where interpolation is to take place. |

`method` |
specifies the interpolation method to be used. Choices
are |

`n` |
If |

`yleft` |
the value to be returned when input |

`yright` |
the value to be returned when input |

`rule` |
an integer (of length 1 or 2) describing how interpolation
is to take place outside the interval [ |

`f` |
for |

`ties` |
handling of tied |

The inputs can contain missing values which are deleted, so at least
two complete `(x, y)`

pairs are required (for ```
method =
"linear"
```

, one otherwise). If there are duplicated (tied) `x`

values and `ties`

contains a function it is applied to the `y`

values for each distinct `x`

value to produce `(x,y)`

pairs
with unique `x`

.
Useful functions in this context include `mean`

,
`min`

, and `max`

.

If `ties = "ordered"`

the `x`

values are assumed to be already
ordered (and unique) and ties are *not* checked but kept if present.
This is the fastest option for large `length(x)`

.

If `ties`

is a `list`

of length two, `ties[[2]]`

must be a function to be applied to ties, see above, but if
`ties[[1]]`

is identical to `"ordered"`

, the `x`

values
are assumed to be sorted and are only checked for ties. Consequently,
`ties = list("ordered", mean)`

will be slightly more efficient than
the default `ties = mean`

in such a case.

The first `y`

value will be used for interpolation to the left and the last
one for interpolation to the right.

`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.

The value returned by `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.

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
*The New S Language*.
Wadsworth & Brooks/Cole.

`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) (amy <- approx(x, y, xout = x)$y) # warning, can be avoided by specifying 'ties=': op <- options(warn=2) # warnings would be error stopifnot(identical(amy, approx(x, y, xout = x, ties=mean)$y)) options(op) # revert (ay <- approx(x, y, xout = x, ties = "ordered")$y) stopifnot(amy == c(1.5,1.5, 3, 5,5,5, 4.5,4.5, 2,2,2), 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

[Package *stats* version 3.6.0 Index]