Last Observation Carried Forward
Generic function for replacing each
NA with the most recent
NA prior to it.
na.locf(object, na.rm = TRUE, ...) ## S3 method for class 'default': na.locf(object, na.rm = TRUE, fromLast, rev, \dots)
- an object.
- logical. Should leading
NAs be removed?
- logical. Causes observations to be carried backward rather
than forward. Default is
FALSE. This corresponds to NOCB (next observation carried backward).
fromLastinstead. This argument will be eliminated in the future in favor of
- further arguments passed to methods.
- An object in which each
NAin the input object is replaced by the most recent non-
NAprior to it. If there are no earlier non-
NAs then the
NAis 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.rm=TRUE, the default, the above implies that the resulting object will have zero rows. Use
na.rm=FALSEto preserve the
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)))