DescTools (version 0.99.14)

LOCF: Last Observation Carried Forward

Description

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

Usage

LOCF(x)

## S3 method for class 'default':
LOCF(x)
## S3 method for class 'data.frame':
LOCF(x)
## S3 method for class 'matrix':
LOCF(x)

Arguments

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

Value

  • a vector with the same dimension as x.

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