Skip to contents

Wrapper of `seq.Date()` and `lubridate::floor_date`

Usage

grid_dates(
  start_date = "1920-01-01",
  end_date = "2020-01-01",
  by = "1 week",
  unit = "week",
  lookback = TRUE,
  week_start = 7
)

Arguments

start_date

starting date

end_date

end date

by

increment of the sequence. Optional. See ‘Details’.

unit

a string, Period object or a date-time object. When a singleton string, it specifies a time unit or a multiple of a unit to be rounded to. Valid base units are second, minute, hour, day, week, month, bimonth, quarter, season, halfyear and year. Arbitrary unique English abbreviations as in the period() constructor are allowed. Rounding to multiples of units (except weeks) is supported.

When unit is a Period object, it is first converted to a string representation which might not be in the same units as the constructor. For example weeks(1) is converted to "7d 0H 0M 0S". Thus, always check the string representation of the period before passing to this function.

When unit is a date-time object rounding is done to the nearest of the elements in unit. If range of unit vector does not cover the range of x ceiling_date() and floor_date() round to the max(x) and min(x) for elements that fall outside of range(unit).

lookback

Logical, should the first value start before `start_date`

week_start

week start day (Default is 7, Sunday. Set lubridate.week.start to override). Full or abbreviated names of the days of the week can be in English or as provided by the current locale.

Value

vector of Dates at the first of each week, month, year

Examples

grid_dates(start_date = "2023-04-01"
, end_date = "2023-05-16")
#> [1] "2023-03-26" "2023-04-02" "2023-04-09" "2023-04-16" "2023-04-23"
#> [6] "2023-04-30" "2023-05-07"

grid_dates(start_date = "2023-04-01"
, end_date = "2023-05-16"
, lookback = FALSE)
#> [1] "2023-04-02" "2023-04-09" "2023-04-16" "2023-04-23" "2023-04-30"
#> [6] "2023-05-07"


grid_dates(start_date = "2020-04-01"
, end_date = "2023-05-16"
, by = "2 months"
, unit = "month")
#>  [1] "2020-04-01" "2020-06-01" "2020-08-01" "2020-10-01" "2020-12-01"
#>  [6] "2021-02-01" "2021-04-01" "2021-06-01" "2021-08-01" "2021-10-01"
#> [11] "2021-12-01" "2022-02-01" "2022-04-01" "2022-06-01" "2022-08-01"
#> [16] "2022-10-01" "2022-12-01" "2023-02-01" "2023-04-01"
grid_dates(start_date = "2020-04-01"
, end_date = "2023-05-16"
, by = "2 months")
#> there may be a mismatch between your grid units in `by` and `unit`
#>  [1] "2020-03-29" "2020-05-31" "2020-07-26" "2020-09-27" "2020-11-29"
#>  [6] "2021-01-31" "2021-03-28" "2021-05-30" "2021-08-01" "2021-09-26"
#> [11] "2021-11-28" "2022-01-30" "2022-03-27" "2022-05-29" "2022-07-31"
#> [16] "2022-09-25" "2022-11-27" "2023-01-29" "2023-03-26"