R/ptd_add_rebase_column.R

Defines functions ptd_add_rebase_column

# adds a column named rebase
# with 1's in any row corresponding to a rebase date
# and 0's everywhere else

ptd_add_rebase_column <- function(.data, date_field, facet_field, rebase) {
  if (is.list(rebase)) {
    rebase_table <- bind_rows(
      lapply(seq_along(rebase), function(i) {
        data.frame(d = to_datetime(rebase[[i]]), f = names(rebase)[[i]], rebase = 1)
      })
    )
    colnames(rebase_table) <- c(date_field, facet_field, "rebase")

    .data <- .data %>%
      left_join(rebase_table, by = c(date_field, facet_field)) %>%
      mutate(across(rebase, ~ ifelse(is.na(.x), 0, 1)))
  } else if (!is.null(rebase)) {
    # in with NULL returns FALSE, so this is suitable even if rebase isn't provided
    .data$rebase <- as.numeric(.data[[date_field]] %in% to_datetime(rebase))
  } else {
    .data$rebase <- 0
  }

  .data
}

Try the NHSRplotthedots package in your browser

Any scripts or data that you put into this service are public.

NHSRplotthedots documentation built on Nov. 4, 2021, 1:07 a.m.