| linear_fill | R Documentation |
Fills gaps (NA values) in a time-dependent variable by
linear interpolation between two points, or carrying forward or backwards
the last or initial values, respectively. It also creates a new variable
indicating the source of the filled values.
linear_fill(
df,
var,
time_index,
interpolate = TRUE,
fill_forward = TRUE,
fill_backward = TRUE,
.by = NULL
)
df |
A tibble data frame containing one observation per row. |
var |
The variable of df containing gaps to be filled. |
time_index |
The time index variable (usually year). |
interpolate |
Logical. If |
fill_forward |
Logical. If |
fill_backward |
Logical. If |
.by |
A character vector with the grouping variables (optional). |
A tibble data frame (ungrouped) where gaps in var have been filled, and a new "source" variable has been created indicating if the value is original or, in case it has been estimated, the gapfilling method that has been used.
sample_tibble <- tibble::tibble(
category = c("a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b"),
year = c(
"2015", "2016", "2017", "2018", "2019", "2020",
"2015", "2016", "2017", "2018", "2019", "2020"
),
value = c(NA, 3, NA, NA, 0, NA, 1, NA, NA, NA, 5, NA),
)
linear_fill(sample_tibble, value, year, .by = c("category"))
linear_fill(
sample_tibble,
value,
year,
interpolate = FALSE,
.by = c("category"),
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.