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.

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)
#> # A tibble: 10 × 11
#>    group sales count on_sale date         day  week month quarter calendar_year
#>    <chr> <dbl> <dbl>   <dbl> <date>     <dbl> <dbl> <dbl>   <dbl>         <dbl>
#>  1 a       101    35       1 2022-06-29     0     0     0       0             0
#>  2 a       102    34       0 2022-04-29   -61    -9    -2       0             0
#>  3 b       103    33       1 2022-03-01  -120   -17    -3      -1             0
#>  4 b       104    32       0 2021-12-29  -182   -26    -6      -2            -1
#>  5 c       105    31       1 2021-10-29  -243   -35    -8      -2            -1
#>  6 c       106    30       0 2021-08-29  -304   -44   -10      -3            -1
#>  7 d       107    29       1 2021-06-29  -365   -52   -12      -4            -1
#>  8 d       108    28       0 2021-04-29  -426   -61   -14      -4            -1
#>  9 e       109    27       1 2021-03-01  -485   -69   -15      -5            -1
#> 10 e       110    26       0 2020-12-29  -547   -78   -18      -6            -2
#> # … with 1 more variable: fiscal_year <dbl>

# if columns overlap, you will see a warning
demo_data() |>
  dplyr::mutate(week = 1) |>
  add_date_columns(date_col = date)
#> Warning: This step produced duplicate names and some fields have new names (see above) 
#> Use 'drop = TRUE' to remove the overlapping columns prior to adding the new date fields
#> New names:
#>  `week` -> `week...6`
#>  `week` -> `week...8`
#> # A tibble: 10 × 12
#>    group sales count on_sale date       week...6   day week...8 month quarter
#>    <chr> <dbl> <dbl>   <dbl> <date>        <dbl> <dbl>    <dbl> <dbl>   <dbl>
#>  1 a       101    35       1 2022-06-29        1     0        0     0       0
#>  2 a       102    34       0 2022-04-29        1   -61       -9    -2       0
#>  3 b       103    33       1 2022-03-01        1  -120      -17    -3      -1
#>  4 b       104    32       0 2021-12-29        1  -182      -26    -6      -2
#>  5 c       105    31       1 2021-10-29        1  -243      -35    -8      -2
#>  6 c       106    30       0 2021-08-29        1  -304      -44   -10      -3
#>  7 d       107    29       1 2021-06-29        1  -365      -52   -12      -4
#>  8 d       108    28       0 2021-04-29        1  -426      -61   -14      -4
#>  9 e       109    27       1 2021-03-01        1  -485      -69   -15      -5
#> 10 e       110    26       0 2020-12-29        1  -547      -78   -18      -6
#> # … with 2 more variables: calendar_year <dbl>, fiscal_year <dbl>

# 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)
#> # A tibble: 10 × 11
#>    group sales count on_sale date         day  week month quarter calendar_year
#>    <chr> <dbl> <dbl>   <dbl> <date>     <dbl> <dbl> <dbl>   <dbl>         <dbl>
#>  1 a       101    35       1 2022-06-29     0     0     0       0             0
#>  2 a       102    34       0 2022-04-29   -61    -9    -2       0             0
#>  3 b       103    33       1 2022-03-01  -120   -17    -3      -1             0
#>  4 b       104    32       0 2021-12-29  -182   -26    -6      -2            -1
#>  5 c       105    31       1 2021-10-29  -243   -35    -8      -2            -1
#>  6 c       106    30       0 2021-08-29  -304   -44   -10      -3            -1
#>  7 d       107    29       1 2021-06-29  -365   -52   -12      -4            -1
#>  8 d       108    28       0 2021-04-29  -426   -61   -14      -4            -1
#>  9 e       109    27       1 2021-03-01  -485   -69   -15      -5            -1
#> 10 e       110    26       0 2020-12-29  -547   -78   -18      -6            -2
#> # … with 1 more variable: fiscal_year <dbl>