zoo (version 1.5-2)

na.locf: 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, ...)
## S3 method for class 'default':
na.locf(object, na.rm = TRUE, fromLast, rev, \dots)

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).

See Also

zoo

Examples

Run this code
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)))

Run the code above in your browser using DataCamp Workspace