na.locf {zoo}R Documentation

Last Observation Carried Forward

Description

Generic function for replacing each NA with the most recent non-NA prior to it.

Usage

na.locf(object, na.rm = TRUE, ...)
## Default S3 method:
na.locf(object, na.rm = TRUE, fromLast, rev, ...)

Arguments

object an object.
na.rm logical. Should leading NAs be removed?
fromLast logical. Causes observations to be carried backward rather than forward. Default is FALSE. This corresponds to NOCB (next observation carried backward).
rev Use fromLast instead. This argument will be eliminated in the future in favor of fromLast.
... further arguments passed to methods.

Value

An object in which each NA in the input object is replaced by the most recent non-NA prior to it. If there are no earlier non-NAs then the NA is omitted (if na.rm = TRUE) or it is not replaced (if na.rm = FALSE).
Note that if a multi-column zoo object has a column entirely composed of NA then with na.rm=TRUE, the default, the above implies that the resulting object will have zero rows. Use na.rm=FALSE to preserve the NA values instead.

See Also

zoo

Examples

az <- zoo(1:6)

bz <- zoo(c(2,NA,1,4,5,2))
na.locf(bz)
na.locf(bz, fromLast = TRUE)

cz <- zoo(c(NA,9,3,2,3,2))
na.locf(cz)

# generate and fill in missing dates
# by merging with a zero width series having those dates
# and then applying na.locf
z <- zoo(c(0.007306621, 0.007659046, 0.007681013,
        0.007817548, 0.007847579, 0.007867313),
        as.Date(c("1993-01-01", "1993-01-09", "1993-01-16",
        "1993-01-23", "1993-01-30", "1993-02-06")))
dd <- seq(start(z), end(z), "day")
na.locf(merge(z, zoo(, dd)))


[Package zoo version 1.6-2 Index]