approx
Interpolation Functions

Description

Computes the piecewise linear or constant function f for which f(x)==y for the entries in the given x and y vector.

Usage

approx(x, y = NULL, xout = NULL, method = "linear", n = 50,
    yleft = NULL, yright = NULL, rule = 1, f = 0, ties = mean, na.rm = TRUE)
approxfun(x, y = NULL, method = "linear",
    yleft = NULL, yright = NULL, rule = 1, f = 0, ties = mean, na.rm = TRUE) 

Arguments

x,y coordinates of points. The coordinates can be given by two vector arguments or by a single argument x, which is a time series, a two-column matrix, or a list containing components named x and y. The y values represent the curve to be approximated as a function of x.
xout the set of x values for which function values are desired. If xout is not specified, the function is approximated at n equally spaced data points, spanning the range of x.

This argument is for approx only. approxfun returns a function to which you can give this set of x values.

method a character describing the method to be used in approximating the function. This must be either "linear" or "constant".
n an integer giving the number of points, evenly spaced between the minimum and maximum values in x, to be used in forming xout. This argument is ignored if xout is specified. This argument is not allowed in approxfun.
yleft the returned value when the input xout value is less than min(x). It handles out-of-domain points. The default value is defined by the rule as below.
yright the returned value when input xout value is greater than max(x). It handles out-of-domain points. The default value is defined by the rule as described below.

rule an integer vector with the length 1 or 2, corresponding to yleft and yright, describing the rule to use for values of xout that are outside the range of x. If the length of rule is 1, then it is extended to 2 and rule[2] <- rule[1].

If rule is 1, NAs are supplied for any such points. If rule is NOT 1, the y values corresponding to the extreme x values are used.

f a number used when method="constant". f determines a blend of the left and right side y values. Suppose we want an interpolated value between x1 and x2 (with corresponding y values y1 and y2). Then the interpolated value is f*y1+(1-f)*y2. Thus, if f=0, the left y-value is used, if f=1, the right y-value, and if f is between 0 and 1, an intermediate value is used.
ties a function (or a string naming a function, or the special string "ordered") to handle tied values in the x value. If the x argument contains any ties, then approx and approxfun replaces the y argument with tapply(y,INDEX=x,FUN=ties) and replaces the x argument with unique(x). It is expected that, for a scalar argument, this function is the identity function and for longer arguments this function returns a scalar.

The special string "ordered" means to use the first of the y values corresponding to a run of identical x values as the uppper anchor when interpolating just below that x value and to use the last of those y values as the lower anchor when interpolating just above that x value. Think of this as the limit as epsilon goes to zero of replacing the repeated value of x with x+epsilon, x+2*epsilon, ..., x+n*epsilon.

na.rm A logical value.
  • If TRUE, then remove all x,y pairs with NA in either the x or the y position.
  • If FALSE, then do not remove them. In that case, an NA in the x argument causes and error. An NA in an element of the y vector causes predictions that depend on that value to be NA.
Value
approxreturns a list with components named x and y. The x component is a copy of the xout argument, and the y component is the interpolation of the input data.
approxfunreturns a function of one argument, v, which performs the intepolation specified.
See Also
findInterval, lowess
Examples
x <- c(1, 7, 8, 10)
y <- c(1, 40, 41, 42)
approx(x, y, xout = 1:10)
tfun <- approxfun(x, y)
tfun(1:5)
tfun(5:10)
approx(x=c(11, 12, 12, 12, 13), y=c(101, 102, -1000, 202, 303), ties="ordered")
tfun <- approxfun(x=c(11,13,17,18,19), c(101,102,NA,103,104), na.rm = FALSE)
tfun(seq(11,19,by=1/2))
Package stats version 6.0.0-69
Package Index