date-sequence | R Documentation |
This is a Date method for the date_seq()
generic.
date_seq()
generates a date (Date) sequence.
When calling date_seq()
, exactly two of the following must be specified:
to
by
total_size
## S3 method for class 'Date'
date_seq(from, ..., to = NULL, by = NULL, total_size = NULL, invalid = NULL)
from |
A date to start the sequence from. |
... |
These dots are for future extensions and must be empty. |
to |
A date to stop the sequence at.
If |
by |
The unit to increment the sequence by. If If
|
total_size |
The size of the resulting sequence. If specified alongside |
invalid |
One of the following invalid date resolution strategies:
Using either If If |
A date vector.
from <- date_build(2019, 1)
to <- date_build(2019, 4)
# Defaults to daily sequence
date_seq(from, to = to, by = 7)
# Use durations to change to monthly or yearly sequences
date_seq(from, to = to, by = duration_months(1))
date_seq(from, by = duration_years(-2), total_size = 3)
# Note that components of `to` more precise than the precision of `by`
# must match `from` exactly. For example, this is not well defined:
from <- date_build(2019, 5, 2)
to <- date_build(2025, 7, 5)
try(date_seq(from, to = to, by = duration_years(1)))
# The month and day components of `to` must match `from`
to <- date_build(2025, 5, 2)
date_seq(from, to = to, by = duration_years(1))
# ---------------------------------------------------------------------------
# Invalid dates must be resolved with the `invalid` argument
from <- date_build(2019, 1, 31)
to <- date_build(2019, 12, 31)
try(date_seq(from, to = to, by = duration_months(1)))
date_seq(from, to = to, by = duration_months(1), invalid = "previous")
# Compare this to the base R result, which is often a source of confusion
seq(from, to = to, by = "1 month")
# This is equivalent to the overflow invalid resolution strategy
date_seq(from, to = to, by = duration_months(1), invalid = "overflow")
# ---------------------------------------------------------------------------
# Usage of `to` and `total_size` must generate a non-fractional sequence
# between `from` and `to`
from <- date_build(2019, 1, 1)
to <- date_build(2019, 1, 4)
# These are fine
date_seq(from, to = to, total_size = 2)
date_seq(from, to = to, total_size = 4)
# But this is not!
try(date_seq(from, to = to, total_size = 3))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.