add_date_columns {headliner}R Documentation

Add columns with date calculations based on reference date

Description

Using a reference date (defaults to current date), columns are appended to the data set describing the number of days, weeks, months, quarters, calendar years and fiscal years since the reference date. If the new columns share names with an existing column, the function will show a warning.

Usage

add_date_columns(
  df,
  date_col,
  ref_date = Sys.Date(),
  fiscal_year_offset = 6,
  week_start = 1,
  drop = FALSE
)

Arguments

df

data frame

date_col

column with class of 'date'

ref_date

reference date for calculations, defaults to current date

fiscal_year_offset

the number of months to offset date, if fiscal year ends in June, use 6

week_start

integer for start of week where Monday = 1 and Sunday = 7

drop

some of the generated fields may match the input data frame. When TRUE, the original columns will be removed and replaced with the new field of the same name. Otherwise, columns with the same name will be appended with a '1'

Value

Returns a data frame with columns appended to describe date distances from a reference date.

Examples

demo_data() |>
  add_date_columns(date_col = date)

# if columns overlap, you will see a warning
demo_data() |>
  dplyr::mutate(week = 1) |>
  add_date_columns(date_col = date)

# to drop the old column and keep the new column use `drop = TRUE`
demo_data() |>
  dplyr::mutate(week = 1) |>
  add_date_columns(date_col = date, drop = TRUE)

[Package headliner version 0.0.3 Index]