Fill with "Last Observation Carried Forward"

fill_locf(body, idx, fail = NA)

Arguments

body

[numeric vector]

The body of the vector.

idx

[integer vector]

the index to replace with.

fail

[numeric(1) or numeric vector: fill]

In case it fails to fill some values.

Value

Returns a vector with the same class and attributes as the input vector.

Examples

x <- c(5,3,2,2,5) lagx(x, n = 2, fill = fill_locf)
#> [1] NA NA 5 3 2
# A not so very neat way to deal with NA when `fill_locf` fails is lagx(x, n = 2, fill = ~ fill_locf(.x,.y, fail = 0))
#> [1] 0 0 5 3 2
leadx(x, n = 2, fill = fill_locf)
#> [1] 2 2 5 5 5
lagx(x, n = 2, fill = fill_nocb)
#> [1] 5 5 5 3 2
leadx(x, n = 2, fill = fill_nocb)
#> [1] 2 2 5 NA NA
leadx(x, n = 2, fill = ~ fill_nocb(.x,.y, fail = 0))
#> [1] 2 2 5 0 0