The counterpart to date_seq_dates(). Take an original set of data and place
it within a regular time series where the periodicity of the time series may
be expressed as numbers of days, weeks, months quarters, or years, and the
periods are defined by an anchoring date, day of the week or by reference to
the start or end of the input dates. This can either return the periods as
dates or factors (e.g. for plotting) or as a time_period
for analysis that
relies on a numeric representation of the date or duration from the anchor.
Usage
cut_date(
dates,
unit,
anchor = "start",
output = c("date", "factor", "time_period"),
dfmt = "%d/%b/%y",
ifmt = "{start} — {end}",
...
)
Arguments
- dates
a set of dates
- unit
a period e.g. "1 week"
- anchor
one of a date, "start" or "end" or a weekday name e.g. "mon" this will always be one of the start of the time periods we are cutting into
- output
return the result as either a "date" (the default), an ordered "factor" with the date ranges as a label, or as a "time_period". The result is named with labels referring to the
- dfmt
the
strptime
format for the dates in the labels- ifmt
a
sprintf
format for the period label containing%s
exactly twice.- ...
ignored
Value
a set of dates, times or a factor level, representing the start of the period the date falls into, where the period is defined by the duration and the anchor
Examples
dates = as.Date(c("2020-01-01","2020-02-01","2020-01-15","2020-02-03",NA))
fs = ggoutbreak::date_seq(dates, "2 days")
dates - cut_date(dates, "2 days")
#> Time differences in days
#> 01/Jan/20 — 02/Jan/20 31/Jan/20 — 01/Feb/20 15/Jan/20 — 16/Jan/20
#> 0 1 0
#> 02/Feb/20 — 03/Feb/20 Unknown
#> 1 NA
cut_date(dates,unit="2 days", output="time_period")
#> time unit: 2 days, origin: 2020-01-01 (a Wednesday)
#> [1] 0 15 7 16 NA
# A weekly set of dates:
dates2 = Sys.Date() + floor(stats::runif(50,max=10))*7
# in this specific situation the final date is not truncated because the
# input data is seen as an exact match for the whole output period.
cut_date(dates2, "1 week", "sun", output="factor")
#> [1] 30/Mar/25 — 05/Apr/25 02/Mar/25 — 08/Mar/25 02/Feb/25 — 08/Feb/25
#> [4] 30/Mar/25 — 05/Apr/25 23/Feb/25 — 01/Mar/25 16/Mar/25 — 22/Mar/25
#> [7] 02/Feb/25 — 08/Feb/25 23/Feb/25 — 01/Mar/25 30/Mar/25 — 05/Apr/25
#> [10] 16/Mar/25 — 22/Mar/25 30/Mar/25 — 05/Apr/25 06/Apr/25 — 12/Apr/25
#> [13] 23/Feb/25 — 01/Mar/25 30/Mar/25 — 05/Apr/25 02/Mar/25 — 08/Mar/25
#> [16] 23/Feb/25 — 01/Mar/25 09/Mar/25 — 15/Mar/25 23/Mar/25 — 29/Mar/25
#> [19] 02/Feb/25 — 08/Feb/25 06/Apr/25 — 12/Apr/25 02/Mar/25 — 08/Mar/25
#> [22] 30/Mar/25 — 05/Apr/25 02/Mar/25 — 08/Mar/25 23/Feb/25 — 01/Mar/25
#> [25] 30/Mar/25 — 05/Apr/25 23/Feb/25 — 01/Mar/25 30/Mar/25 — 05/Apr/25
#> [28] 23/Mar/25 — 29/Mar/25 23/Mar/25 — 29/Mar/25 16/Mar/25 — 22/Mar/25
#> [31] 16/Feb/25 — 22/Feb/25 06/Apr/25 — 12/Apr/25 23/Mar/25 — 29/Mar/25
#> [34] 16/Feb/25 — 22/Feb/25 02/Feb/25 — 08/Feb/25 09/Mar/25 — 15/Mar/25
#> [37] 02/Mar/25 — 08/Mar/25 09/Mar/25 — 15/Mar/25 23/Feb/25 — 01/Mar/25
#> [40] 16/Feb/25 — 22/Feb/25 23/Mar/25 — 29/Mar/25 16/Feb/25 — 22/Feb/25
#> [43] 06/Apr/25 — 12/Apr/25 02/Mar/25 — 08/Mar/25 06/Apr/25 — 12/Apr/25
#> [46] 16/Mar/25 — 22/Mar/25 23/Feb/25 — 01/Mar/25 16/Mar/25 — 22/Mar/25
#> [49] 06/Apr/25 — 12/Apr/25 09/Mar/25 — 15/Mar/25
#> 11 Levels: 02/Feb/25 — 08/Feb/25 < ... < 13/Apr/25 — 19/Apr/25
cut_date(dates2, dfmt = "%d/%b", output="factor", unit = "2 weeks", anchor="sun")
#> [1] 30/Mar — 12/Apr 02/Mar — 15/Mar 02/Feb — 15/Feb 30/Mar — 12/Apr
#> [5] 16/Feb — 01/Mar 16/Mar — 29/Mar 02/Feb — 15/Feb 16/Feb — 01/Mar
#> [9] 30/Mar — 12/Apr 16/Mar — 29/Mar 30/Mar — 12/Apr 30/Mar — 12/Apr
#> [13] 16/Feb — 01/Mar 30/Mar — 12/Apr 02/Mar — 15/Mar 16/Feb — 01/Mar
#> [17] 02/Mar — 15/Mar 16/Mar — 29/Mar 02/Feb — 15/Feb 30/Mar — 12/Apr
#> [21] 02/Mar — 15/Mar 30/Mar — 12/Apr 02/Mar — 15/Mar 16/Feb — 01/Mar
#> [25] 30/Mar — 12/Apr 16/Feb — 01/Mar 30/Mar — 12/Apr 16/Mar — 29/Mar
#> [29] 16/Mar — 29/Mar 16/Mar — 29/Mar 16/Feb — 01/Mar 30/Mar — 12/Apr
#> [33] 16/Mar — 29/Mar 16/Feb — 01/Mar 02/Feb — 15/Feb 02/Mar — 15/Mar
#> [37] 02/Mar — 15/Mar 02/Mar — 15/Mar 16/Feb — 01/Mar 16/Feb — 01/Mar
#> [41] 16/Mar — 29/Mar 16/Feb — 01/Mar 30/Mar — 12/Apr 02/Mar — 15/Mar
#> [45] 30/Mar — 12/Apr 16/Mar — 29/Mar 16/Feb — 01/Mar 16/Mar — 29/Mar
#> [49] 30/Mar — 12/Apr 02/Mar — 15/Mar
#> 6 Levels: 02/Feb — 15/Feb < 16/Feb — 01/Mar < ... < 13/Apr — 26/Apr