na.approx {zoo} | R Documentation |
Generic functions for replacing each NA
with interpolated
values.
na.approx(object, ...) ## Default S3 method: na.approx(object, along = index(object), na.rm = TRUE, ...) na.spline(object, ...) ## Default S3 method: na.spline(object, along = index(object), na.rm = TRUE, ...)
object |
object in which NA s are to be replaced |
along |
variable to use for interpolation. Has to be numeric, is otherwise coerced to numeric. |
na.rm |
logical. Should leading NA s be removed? |
... |
further arguments passed to methods. |
Missing values (NA
s) are replaced by linear interpolation via
approx
or cubic spline interpolation via spline
,
respectively.
By default the index associated with object
is used
for interpolation. Note, that if this calls index.default
this gives an equidistant spacing 1:NROW(object)
. If object
is a matrix or data.frame, the interpolation is done separately for
each column.
An object in which each NA
in the input object is replaced
by interpolating the non-NA
values before and after it.
Leading NA
s are omitted (if na.rm = TRUE
) or not replaced (if na.rm = FALSE
).
zoo
, approx
, na.contiguous
, link{na.locf}
, na.omit
, na.trim
, spline
, stinterp
z <- zoo(c(2,NA,1,4,5,2), c(1,3,4,6,7,8)) ## use underlying time scale for interpolation na.approx(z) ## use equidistant spacing na.approx(z, 1:6) # with and without na.rm = FALSE zz <- c(NA,9,3,NA,3,2) na.approx(zz, na.rm = FALSE) na.approx(zz)