

approx[fun] {base}                           R Documentation

_I_n_t_e_r_p_o_l_a_t_i_o_n _F_u_n_c_t_i_o_n_s

_U_s_a_g_e_:

     approx   (x, y, xout, method="linear", n=50,
               yleft, yright, rule=1, f=0)
     approxfun(x, y,       method="linear",
               yleft, yright, rule=1, f=0)

_A_r_g_u_m_e_n_t_s_:

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

    xout: an optional set of values specifying where inter-
          polation is to take place.

  method: specifies the interpolation method to be used.
          Choices are `"linear"' or `"constant"'.

       n: If `xout' is not specified, interpolation takes
          place at `n' equally spaced points spanning the
          interval [`min(x)', `max(x)'].

   yleft: the value to be returned when input `x' values
          less than `min(x)'. The default is defined by the
          value of `rule' given below.

  yright: the value to be returned when input `x' values
          greater than `max(x)'. The default is defined by
          the value of `rule' given below.

    rule: an integer describing how interpolation is to take
          place outside the interval [`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.

       f: For `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*f+y1*(1-f)' so that
          `f=0' is right-continuous and `f=1' is left-con-
          tinuous.

_D_e_t_a_i_l_s_:

     The inputs can contain missing values which are delted,
     so at least two complete `(x, y)' pairs are required.

_V_a_l_u_e_:

     `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.
     This is often more useful than `approx'.

_S_e_e _A_l_s_o_:

     `spline' and `splinefun' for spline interpolation.

_E_x_a_m_p_l_e_s_:

     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, 10, col = "green")
     points(x, y)
     is.function(fc <- approxfun(x, y, method = "const"))  # T
     curve(fc(x), 0, 10, col = "darkblue", add = TRUE)

