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] 19/Oct/25 — 25/Oct/25 05/Oct/25 — 11/Oct/25 24/Aug/25 — 30/Aug/25
#> [4] 31/Aug/25 — 06/Sep/25 14/Sep/25 — 20/Sep/25 19/Oct/25 — 25/Oct/25
#> [7] 07/Sep/25 — 13/Sep/25 14/Sep/25 — 20/Sep/25 24/Aug/25 — 30/Aug/25
#> [10] 28/Sep/25 — 04/Oct/25 12/Oct/25 — 18/Oct/25 12/Oct/25 — 18/Oct/25
#> [13] 14/Sep/25 — 20/Sep/25 28/Sep/25 — 04/Oct/25 05/Oct/25 — 11/Oct/25
#> [16] 21/Sep/25 — 27/Sep/25 12/Oct/25 — 18/Oct/25 19/Oct/25 — 25/Oct/25
#> [19] 24/Aug/25 — 30/Aug/25 07/Sep/25 — 13/Sep/25 24/Aug/25 — 30/Aug/25
#> [22] 07/Sep/25 — 13/Sep/25 28/Sep/25 — 04/Oct/25 28/Sep/25 — 04/Oct/25
#> [25] 12/Oct/25 — 18/Oct/25 07/Sep/25 — 13/Sep/25 05/Oct/25 — 11/Oct/25
#> [28] 17/Aug/25 — 23/Aug/25 12/Oct/25 — 18/Oct/25 31/Aug/25 — 06/Sep/25
#> [31] 05/Oct/25 — 11/Oct/25 07/Sep/25 — 13/Sep/25 28/Sep/25 — 04/Oct/25
#> [34] 24/Aug/25 — 30/Aug/25 05/Oct/25 — 11/Oct/25 21/Sep/25 — 27/Sep/25
#> [37] 21/Sep/25 — 27/Sep/25 05/Oct/25 — 11/Oct/25 07/Sep/25 — 13/Sep/25
#> [40] 12/Oct/25 — 18/Oct/25 17/Aug/25 — 23/Aug/25 24/Aug/25 — 30/Aug/25
#> [43] 17/Aug/25 — 23/Aug/25 07/Sep/25 — 13/Sep/25 19/Oct/25 — 25/Oct/25
#> [46] 19/Oct/25 — 25/Oct/25 24/Aug/25 — 30/Aug/25 21/Sep/25 — 27/Sep/25
#> [49] 24/Aug/25 — 30/Aug/25 21/Sep/25 — 27/Sep/25
#> 11 Levels: 17/Aug/25 — 23/Aug/25 < ... < 26/Oct/25 — 01/Nov/25
cut_date(dates2, dfmt = "%d/%b", output="factor", unit = "2 weeks", anchor="sun")
#> [1] 12/Oct — 25/Oct 28/Sep — 11/Oct 17/Aug — 30/Aug 31/Aug — 13/Sep
#> [5] 14/Sep — 27/Sep 12/Oct — 25/Oct 31/Aug — 13/Sep 14/Sep — 27/Sep
#> [9] 17/Aug — 30/Aug 28/Sep — 11/Oct 12/Oct — 25/Oct 12/Oct — 25/Oct
#> [13] 14/Sep — 27/Sep 28/Sep — 11/Oct 28/Sep — 11/Oct 14/Sep — 27/Sep
#> [17] 12/Oct — 25/Oct 12/Oct — 25/Oct 17/Aug — 30/Aug 31/Aug — 13/Sep
#> [21] 17/Aug — 30/Aug 31/Aug — 13/Sep 28/Sep — 11/Oct 28/Sep — 11/Oct
#> [25] 12/Oct — 25/Oct 31/Aug — 13/Sep 28/Sep — 11/Oct 17/Aug — 30/Aug
#> [29] 12/Oct — 25/Oct 31/Aug — 13/Sep 28/Sep — 11/Oct 31/Aug — 13/Sep
#> [33] 28/Sep — 11/Oct 17/Aug — 30/Aug 28/Sep — 11/Oct 14/Sep — 27/Sep
#> [37] 14/Sep — 27/Sep 28/Sep — 11/Oct 31/Aug — 13/Sep 12/Oct — 25/Oct
#> [41] 17/Aug — 30/Aug 17/Aug — 30/Aug 17/Aug — 30/Aug 31/Aug — 13/Sep
#> [45] 12/Oct — 25/Oct 12/Oct — 25/Oct 17/Aug — 30/Aug 14/Sep — 27/Sep
#> [49] 17/Aug — 30/Aug 14/Sep — 27/Sep
#> 6 Levels: 17/Aug — 30/Aug < 31/Aug — 13/Sep < ... < 26/Oct — 08/Nov