Fill NA with last non-NA element.

fill_run(x, run_for_first = FALSE, only_within = FALSE)

Arguments

x

(vector, data.frame, matrix, xts, grouped_df)
Input in runner custom function f.

run_for_first

If first elements are filled with NA, run_for_first = TRUE allows to fill all initial NA with nearest non-NA value. By default run_for_first = TRUE

only_within

NA are replaced only if previous and next non-NA values are the same. By default only_within = TRUE

Value

vector - x containing all x elements with NA

replaced with previous non-NA element.

Examples

fill_run(c(NA, NA, 1:10, NA, NA), run_for_first = TRUE)
#>  [1]  1  1  1  2  3  4  5  6  7  8  9 10 10 10
fill_run(c(NA, NA, 1:10, NA, NA), run_for_first = TRUE)
#>  [1]  1  1  1  2  3  4  5  6  7  8  9 10 10 10
fill_run(c(NA, NA, 1:10, NA, NA), run_for_first = FALSE)
#>  [1] NA NA  1  2  3  4  5  6  7  8  9 10 10 10
fill_run(c(NA, NA, 1, 2, NA, NA, 2, 2, NA, NA, 1, NA, NA), run_for_first = TRUE, only_within = TRUE)
#>  [1]  1  1  1  2  2  2  2  2 NA NA  1 NA NA