locf: Fill NA values in a vector using previous non-NA value

locfR Documentation

Fill NA values in a vector using previous non-NA value

Description

Fill NA values in a vector using previous non-NA value

Usage

locf(x, na = NA, cond = T, backfill = T)

Arguments

x

A vector of values that may have NA values to fill

na

A scalar or vector of values to treat as NA for purposes of filling

cond

A logical scalar or vector. NA will not be replaced if the corresponding condition is F.

backfill

A logical indicating if initial NA values should be backfilled from the first non-NA value.

Details

This is an interpolation method called Last Observation Carried Forward. It fills in a missing or NA value with the previous non-NA value. If backfill=TRUE (default), any initial NA values will be filled backward from the first non-NA value. Multiple values can be provided to consider as a match for NA.

Value

The x vector with NA values replaced by previous non-NA value

Examples

x = c(1,"",2,3,"",".")
locf(x, na=c("","."))
locf(x, na=c("","."), cond=c(TRUE,TRUE,TRUE,TRUE,FALSE,TRUE))
locf(c(NA,NA,1,2,NA,NA,3))
locf(c(NA,NA,1,2,NA,NA,3), backfill=FALSE)

qPharmetra/PMDatR documentation built on April 7, 2024, 5:42 p.m.