Replace Missing Values Using Last Observation Carried Forward
Source:R/replace_na_locf.R
replace_na_locf.Rd
Replaces missing values by carrying forward the last observed value, with control over both minimum and maximum gap sizes to fill.
Arguments
- x
A vector containing numeric data with missing values (NAs)
- min_gap
Integer specifying minimum gap size to fill. Gaps shorter than this will be left as NA. Default is 1 (fill all gaps).
- max_gap
Integer or Inf specifying maximum gap size to fill. Gaps longer than this will be left as NA. Default is Inf (no upper limit).
Value
A numeric vector with NA values replaced by the last observed value where gap length criteria are met.
Details
The function applies both minimum and maximum gap criteria:
Gaps shorter than min_gap are left as NA
Gaps longer than max_gap are left as NA
Only gaps that meet both criteria are filled If both parameters are specified, min_gap must be less than or equal to max_gap.
Examples
if (FALSE) { # \dontrun{
x <- c(1, NA, NA, 4, 5, NA, NA, NA, 9)
replace_na_locf(x) # fills all gaps
replace_na_locf(x, min_gap = 2) # only gaps >= 2
replace_na_locf(x, max_gap = 2) # only gaps <= 2
replace_na_locf(x, min_gap = 2, max_gap = 3) # gaps between 2 and 3
} # }