Skip to contents

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)
#> # 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-12-19     0     0     0       0             0
#>  2 a       102    34       0 2022-10-19   -61    -9    -2       0             0
#>  3 b       103    33       1 2022-08-19  -122   -18    -4      -1             0
#>  4 b       104    32       0 2022-06-19  -183   -27    -6      -2             0
#>  5 c       105    31       1 2022-04-19  -244   -35    -8      -2             0
#>  6 c       106    30       0 2022-02-19  -303   -44   -10      -3             0
#>  7 d       107    29       1 2021-12-19  -365   -53   -12      -4            -1
#>  8 d       108    28       0 2021-10-19  -426   -61   -14      -4            -1
#>  9 e       109    27       1 2021-08-19  -487   -70   -16      -5            -1
#> 10 e       110    26       0 2021-06-19  -548   -79   -18      -6            -1
#> # … 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-12-19        1     0        0     0       0
#>  2 a       102    34       0 2022-10-19        1   -61       -9    -2       0
#>  3 b       103    33       1 2022-08-19        1  -122      -18    -4      -1
#>  4 b       104    32       0 2022-06-19        1  -183      -27    -6      -2
#>  5 c       105    31       1 2022-04-19        1  -244      -35    -8      -2
#>  6 c       106    30       0 2022-02-19        1  -303      -44   -10      -3
#>  7 d       107    29       1 2021-12-19        1  -365      -53   -12      -4
#>  8 d       108    28       0 2021-10-19        1  -426      -61   -14      -4
#>  9 e       109    27       1 2021-08-19        1  -487      -70   -16      -5
#> 10 e       110    26       0 2021-06-19        1  -548      -79   -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-12-19     0     0     0       0             0
#>  2 a       102    34       0 2022-10-19   -61    -9    -2       0             0
#>  3 b       103    33       1 2022-08-19  -122   -18    -4      -1             0
#>  4 b       104    32       0 2022-06-19  -183   -27    -6      -2             0
#>  5 c       105    31       1 2022-04-19  -244   -35    -8      -2             0
#>  6 c       106    30       0 2022-02-19  -303   -44   -10      -3             0
#>  7 d       107    29       1 2021-12-19  -365   -53   -12      -4            -1
#>  8 d       108    28       0 2021-10-19  -426   -61   -14      -4            -1
#>  9 e       109    27       1 2021-08-19  -487   -70   -16      -5            -1
#> 10 e       110    26       0 2021-06-19  -548   -79   -18      -6            -1
#> # … with 1 more variable: fiscal_year <dbl>