Quickly create a sequence of dates from minimal specifications.
span_date(from, to = NULL, len_out = NULL, by = NULL)
from | Integer or character of length 4 (yyyy), 6 (yyyymm), or 8 (yyymmdd). Indicating the start value of the sequence. |
---|---|
to | Integer or character of length 4 (yyyy), 6 (yyyymm), or 8 (yyymmdd). Optional. |
len_out | The desired length of the sequence. Optional. |
by | The desired interval. Optional. |
An object of class Date.
Minimal specification of dates, sets unspecified date parts to default values. These are 01 for both month and day.
In addition to from
, either to
or len_out
must be specified.
If by
is not specified, span_date
will set the interval to the
highest of the specified date parts in either from
or to
.
For example, if they are 2011 and 2015 it will be "year", if they are 2011
and 201501 it will be "month".
# using "to" argument span_date(2011, 2015)#> [1] "2011-01-01" "2012-01-01" "2013-01-01" "2014-01-01" "2015-01-01"span_date(201101, 201501)#> [1] "2011-01-01" "2011-02-01" "2011-03-01" "2011-04-01" "2011-05-01" #> [6] "2011-06-01" "2011-07-01" "2011-08-01" "2011-09-01" "2011-10-01" #> [11] "2011-11-01" "2011-12-01" "2012-01-01" "2012-02-01" "2012-03-01" #> [16] "2012-04-01" "2012-05-01" "2012-06-01" "2012-07-01" "2012-08-01" #> [21] "2012-09-01" "2012-10-01" "2012-11-01" "2012-12-01" "2013-01-01" #> [26] "2013-02-01" "2013-03-01" "2013-04-01" "2013-05-01" "2013-06-01" #> [31] "2013-07-01" "2013-08-01" "2013-09-01" "2013-10-01" "2013-11-01" #> [36] "2013-12-01" "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01" #> [41] "2014-05-01" "2014-06-01" "2014-07-01" "2014-08-01" "2014-09-01" #> [46] "2014-10-01" "2014-11-01" "2014-12-01" "2015-01-01"span_date(2011, 2015, by = "month")#> [1] "2011-01-01" "2011-02-01" "2011-03-01" "2011-04-01" "2011-05-01" #> [6] "2011-06-01" "2011-07-01" "2011-08-01" "2011-09-01" "2011-10-01" #> [11] "2011-11-01" "2011-12-01" "2012-01-01" "2012-02-01" "2012-03-01" #> [16] "2012-04-01" "2012-05-01" "2012-06-01" "2012-07-01" "2012-08-01" #> [21] "2012-09-01" "2012-10-01" "2012-11-01" "2012-12-01" "2013-01-01" #> [26] "2013-02-01" "2013-03-01" "2013-04-01" "2013-05-01" "2013-06-01" #> [31] "2013-07-01" "2013-08-01" "2013-09-01" "2013-10-01" "2013-11-01" #> [36] "2013-12-01" "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01" #> [41] "2014-05-01" "2014-06-01" "2014-07-01" "2014-08-01" "2014-09-01" #> [46] "2014-10-01" "2014-11-01" "2014-12-01" "2015-01-01"span_date(2011, 201501)#> [1] "2011-01-01" "2011-02-01" "2011-03-01" "2011-04-01" "2011-05-01" #> [6] "2011-06-01" "2011-07-01" "2011-08-01" "2011-09-01" "2011-10-01" #> [11] "2011-11-01" "2011-12-01" "2012-01-01" "2012-02-01" "2012-03-01" #> [16] "2012-04-01" "2012-05-01" "2012-06-01" "2012-07-01" "2012-08-01" #> [21] "2012-09-01" "2012-10-01" "2012-11-01" "2012-12-01" "2013-01-01" #> [26] "2013-02-01" "2013-03-01" "2013-04-01" "2013-05-01" "2013-06-01" #> [31] "2013-07-01" "2013-08-01" "2013-09-01" "2013-10-01" "2013-11-01" #> [36] "2013-12-01" "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01" #> [41] "2014-05-01" "2014-06-01" "2014-07-01" "2014-08-01" "2014-09-01" #> [46] "2014-10-01" "2014-11-01" "2014-12-01" "2015-01-01"span_date(20111225, 2012)#> [1] "2011-12-25" "2011-12-26" "2011-12-27" "2011-12-28" "2011-12-29" #> [6] "2011-12-30" "2011-12-31" "2012-01-01"# using "len_out" argument span_date(2011, len_out = 4)#> [1] "2011-01-01" "2012-01-01" "2013-01-01" "2014-01-01"span_date(201101, len_out = 4)#> [1] "2011-01-01" "2011-02-01" "2011-03-01" "2011-04-01"span_date(20110101, len_out = 4)#> [1] "2011-01-01" "2011-01-02" "2011-01-03" "2011-01-04"span_date(20110101, len_out = 4, by = "month")#> [1] "2011-01-01" "2011-02-01" "2011-03-01" "2011-04-01"