DescTools (version 0.99.18)

LOCF: Last Observation Carried Forward

Description

Replace NAs by the last observed value (aka "Last Observation Carried Forward").

Usage

LOCF(x)
"LOCF"(x) "LOCF"(x) "LOCF"(x)

Arguments

x
a vector, a data.frame or a matrix containing NAs.

Value

Details

The function will replace all NAs found in a vector with the last earlier value not being NA. In data.frames each column will be treated as described.

See Also

See also the package Hmisc for less coarse imputation functions.

Examples

Run this code
d.frm <- data.frame(
  tag=rep(c("mo", "di", "mi", "do", "fr", "sa", "so"), 4)
, val=rep(c(runif(5), rep(NA,2)), 4) )

d.frm$locf <- LOCF( d.frm$val )
d.frm

Run the code above in your browser using DataCamp Workspace