Skip to contents

where the periodicity of the time series is expressed as numbers of days, weeks, months quarters, or years.

Usage

cut_date(dates, full_seq = dates, factor = FALSE, ...)

Arguments

dates

a set of dates

full_seq

a full sequence of allowable dates as created by 'full_seq_dates()'. Alternatively a vector of dates will some regular periodicity, that will be used as an input for 'full_seq_dates()', if missing this will be derived from the data itself.

factor

return the result as an ordered factor with the date ranges as a label. if false this returns a date vector where the date is

...

if full_seq is not give, or a plain vector of dates, other options for 'full_seq_dates()' can be set here. E.g. ('fmt="%d/%m/%Y", period="1 week")

Value

a set of dates, representing the start (or end) of the period the date falls into, where the period is defined by 'full_seq' - which is usually defined by 'full_seq_dates()'

Examples

# dates = as.Date(c("2020-01-01","2020-02-01","2020-01-15","2020-02-03",NA))
# fs = full_seq_dates(dates, "2 days")
# dates - cut_date(dates, fs)
# cut_date(dates,fs,TRUE)

# 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, fmt = "%d/%b", factor = TRUE)

# if the input dates don't line up with the output dates
# there may be incomplete coverage of the first and last category.
# where the cutting results in short periods. In this
# instance the first and last periods are truncated to prevent them
# being counted as complete when they are in fact potentially missing a few days worth of data:
# cut_date(dates2, fmt = "%d/%b", factor = TRUE, period = "-2 weeks", anchor="sun")