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