R/fill_time_series.R

Defines functions complete_years

Documented in complete_years

#' @title Complete Years Time Series
#'
#' @description Fills gaps in time series data of years that might occur due to missing
#' data or dates in task lists.
#'
#' @param dfr A data frame with a column for year numbers.
#' @param year_col A bare column name (defaults to year) to expand to a full
#'    sequence of year numbers.
#'
#' @return The expanded `dfr` with a column variable `year_col` that contains
#'    a full sequence of year numbers.
#'
#' @importFrom dplyr %>%
#
#' @export
complete_years <- function(dfr, year_col = year) {

  years_observed <- dplyr::distinct(dfr, {{ year_col}} ) %>% pull( {{ year_col}} )
  all_years <- tidyr::full_seq(years_observed, period = 1)

  full_dfr <- dplyr::full_join(x = dfr,
                               y = tibble::tibble( {{ year_col }} := all_years),
                               ## Use non-idiomatic way because idiomatic rlang won't fly
                               by = deparse(substitute(year_col)))

  return(full_dfr)
}
paullemmens/todoRtxt documentation built on May 19, 2023, 3:21 p.m.