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
strptimeformat for the dates in the labels- ifmt
a
sprintfformat for the period label containing%sexactly 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/Nov/25 — 06/Dec/25 16/Nov/25 — 22/Nov/25 05/Oct/25 — 11/Oct/25
#> [4] 12/Oct/25 — 18/Oct/25 26/Oct/25 — 01/Nov/25 30/Nov/25 — 06/Dec/25
#> [7] 19/Oct/25 — 25/Oct/25 26/Oct/25 — 01/Nov/25 05/Oct/25 — 11/Oct/25
#> [10] 09/Nov/25 — 15/Nov/25 23/Nov/25 — 29/Nov/25 23/Nov/25 — 29/Nov/25
#> [13] 26/Oct/25 — 01/Nov/25 09/Nov/25 — 15/Nov/25 16/Nov/25 — 22/Nov/25
#> [16] 02/Nov/25 — 08/Nov/25 23/Nov/25 — 29/Nov/25 30/Nov/25 — 06/Dec/25
#> [19] 05/Oct/25 — 11/Oct/25 19/Oct/25 — 25/Oct/25 05/Oct/25 — 11/Oct/25
#> [22] 19/Oct/25 — 25/Oct/25 09/Nov/25 — 15/Nov/25 09/Nov/25 — 15/Nov/25
#> [25] 23/Nov/25 — 29/Nov/25 19/Oct/25 — 25/Oct/25 16/Nov/25 — 22/Nov/25
#> [28] <NA> 23/Nov/25 — 29/Nov/25 12/Oct/25 — 18/Oct/25
#> [31] 16/Nov/25 — 22/Nov/25 19/Oct/25 — 25/Oct/25 09/Nov/25 — 15/Nov/25
#> [34] 05/Oct/25 — 11/Oct/25 16/Nov/25 — 22/Nov/25 02/Nov/25 — 08/Nov/25
#> [37] 02/Nov/25 — 08/Nov/25 16/Nov/25 — 22/Nov/25 19/Oct/25 — 25/Oct/25
#> [40] 23/Nov/25 — 29/Nov/25 <NA> 05/Oct/25 — 11/Oct/25
#> [43] <NA> 19/Oct/25 — 25/Oct/25 30/Nov/25 — 06/Dec/25
#> [46] 30/Nov/25 — 06/Dec/25 05/Oct/25 — 11/Oct/25 02/Nov/25 — 08/Nov/25
#> [49] 05/Oct/25 — 11/Oct/25 02/Nov/25 — 08/Nov/25
#> 10 Levels: 05/Oct/25 — 11/Oct/25 < ... < 07/Dec/25 — 13/Dec/25
cut_date(dates2, dfmt = "%d/%b", output="factor", unit = "2 weeks", anchor="sun")
#> [1] 23/Nov — 06/Dec 09/Nov — 22/Nov 28/Sep — 11/Oct 12/Oct — 25/Oct
#> [5] 26/Oct — 08/Nov 23/Nov — 06/Dec 12/Oct — 25/Oct 26/Oct — 08/Nov
#> [9] 28/Sep — 11/Oct 09/Nov — 22/Nov 23/Nov — 06/Dec 23/Nov — 06/Dec
#> [13] 26/Oct — 08/Nov 09/Nov — 22/Nov 09/Nov — 22/Nov 26/Oct — 08/Nov
#> [17] 23/Nov — 06/Dec 23/Nov — 06/Dec 28/Sep — 11/Oct 12/Oct — 25/Oct
#> [21] 28/Sep — 11/Oct 12/Oct — 25/Oct 09/Nov — 22/Nov 09/Nov — 22/Nov
#> [25] 23/Nov — 06/Dec 12/Oct — 25/Oct 09/Nov — 22/Nov 28/Sep — 11/Oct
#> [29] 23/Nov — 06/Dec 12/Oct — 25/Oct 09/Nov — 22/Nov 12/Oct — 25/Oct
#> [33] 09/Nov — 22/Nov 28/Sep — 11/Oct 09/Nov — 22/Nov 26/Oct — 08/Nov
#> [37] 26/Oct — 08/Nov 09/Nov — 22/Nov 12/Oct — 25/Oct 23/Nov — 06/Dec
#> [41] 28/Sep — 11/Oct 28/Sep — 11/Oct 28/Sep — 11/Oct 12/Oct — 25/Oct
#> [45] 23/Nov — 06/Dec 23/Nov — 06/Dec 28/Sep — 11/Oct 26/Oct — 08/Nov
#> [49] 28/Sep — 11/Oct 26/Oct — 08/Nov
#> 6 Levels: 28/Sep — 11/Oct < 12/Oct — 25/Oct < ... < 07/Dec — 20/Dec
