lvcf: Last Value Carried Forward

View source: R/xtras.R

lvcfR Documentation

Last Value Carried Forward

Description

Replace missing values in a covariate with the last non-missing value, separately for each id.

Usage

lvcf(id, x, time)

Arguments

id

subject identifier

x

the covariate which will be modified

time

optional; used to sort observations within subject

Details

If an analysis includes a covariate that was measured repeatedly, cholesterol say, the data may often include visits where this was not measured. It is convenient in that case to use, for each subject, the most recently measured value. When a value is extended beyond the last measurement, rather that just replacement of intermediate values, there is the potential for bias, e.g., if the reason for discontinuation is predictive of the future trajectory of values.

If time is missing, then the order of the values within subject is assumed to be the calendar time order. It is not necessary for the the data to be sorted by id.

Value

an updated copy of x

Note

Other R packages also implement an locf function, e.g., na.locf in the zoo package or LOCF in DescTools. However, they do not include the id argument, so per subjct application requires further processing. The tdc operation in tmerge also performs lvcf substitution.

We have used the phrase "last value" rather than "last observation" since this function only addresses a single variable with each call, not the entire row of a data frame (observation) as some others do.

Author(s)

Terry Therneau

See Also

tmerge

Examples

newplat <- with(pbcseq, lvcf(id, platelet))
table(is.na(pbcseq$platelet), is.na(newplat))

survival documentation built on Jan. 16, 2026, 5:07 p.m.