Span a vector of specified interval around a variable of class Date, POSIXct, or POSIXlt..

span_around(x, interval, start_shift = NULL, end_shift = start_shift)

Arguments

x

A vector of class Date, POSIXct, or POSIXlt.

interval

Character, specifying the desired interval.

start_shift

Character, indicating the time to shift back from the first observation.

end_shift

Character, indicating the time to shift forward from the last observation.

Value

A datetime vector, with the first observation smaller or equal than min(x) and the last observation larger or equal than max(x). Spaces between points are equal to interval.

Details

Note that use of the start_shift and end_shift arguments change the entire spanning when they are not in line with the interval. It is not checked for.

Examples

span_around(coffee$time_stamp, "hour")
#> [1] "2016-07-07 09:00:00 CEST" "2016-07-07 10:00:00 CEST" #> [3] "2016-07-07 11:00:00 CEST" "2016-07-07 12:00:00 CEST" #> [5] "2016-07-07 13:00:00 CEST" "2016-07-07 14:00:00 CEST" #> [7] "2016-07-07 15:00:00 CEST" "2016-07-07 16:00:00 CEST" #> [9] "2016-07-07 17:00:00 CEST" "2016-07-07 18:00:00 CEST" #> [11] "2016-07-07 19:00:00 CEST" "2016-07-07 20:00:00 CEST" #> [13] "2016-07-07 21:00:00 CEST" "2016-07-07 22:00:00 CEST" #> [15] "2016-07-07 23:00:00 CEST" "2016-07-08 00:00:00 CEST" #> [17] "2016-07-08 01:00:00 CEST" "2016-07-08 02:00:00 CEST" #> [19] "2016-07-08 03:00:00 CEST" "2016-07-08 04:00:00 CEST" #> [21] "2016-07-08 05:00:00 CEST" "2016-07-08 06:00:00 CEST" #> [23] "2016-07-08 07:00:00 CEST" "2016-07-08 08:00:00 CEST" #> [25] "2016-07-08 09:00:00 CEST" "2016-07-08 10:00:00 CEST" #> [27] "2016-07-08 11:00:00 CEST" "2016-07-08 12:00:00 CEST" #> [29] "2016-07-08 13:00:00 CEST" "2016-07-08 14:00:00 CEST" #> [31] "2016-07-08 15:00:00 CEST" "2016-07-08 16:00:00 CEST" #> [33] "2016-07-08 17:00:00 CEST" "2016-07-08 18:00:00 CEST" #> [35] "2016-07-08 19:00:00 CEST" "2016-07-08 20:00:00 CEST" #> [37] "2016-07-08 21:00:00 CEST" "2016-07-08 22:00:00 CEST" #> [39] "2016-07-08 23:00:00 CEST" "2016-07-09 00:00:00 CEST" #> [41] "2016-07-09 01:00:00 CEST" "2016-07-09 02:00:00 CEST" #> [43] "2016-07-09 03:00:00 CEST" "2016-07-09 04:00:00 CEST" #> [45] "2016-07-09 05:00:00 CEST" "2016-07-09 06:00:00 CEST" #> [47] "2016-07-09 07:00:00 CEST" "2016-07-09 08:00:00 CEST" #> [49] "2016-07-09 09:00:00 CEST" "2016-07-09 10:00:00 CEST" #> [51] "2016-07-09 11:00:00 CEST" "2016-07-09 12:00:00 CEST" #> [53] "2016-07-09 13:00:00 CEST" "2016-07-09 14:00:00 CEST" #> [55] "2016-07-09 15:00:00 CEST" "2016-07-09 16:00:00 CEST" #> [57] "2016-07-09 17:00:00 CEST" "2016-07-09 18:00:00 CEST" #> [59] "2016-07-09 19:00:00 CEST" "2016-07-09 20:00:00 CEST" #> [61] "2016-07-09 21:00:00 CEST" "2016-07-09 22:00:00 CEST" #> [63] "2016-07-09 23:00:00 CEST" "2016-07-10 00:00:00 CEST" #> [65] "2016-07-10 01:00:00 CEST" "2016-07-10 02:00:00 CEST" #> [67] "2016-07-10 03:00:00 CEST" "2016-07-10 04:00:00 CEST" #> [69] "2016-07-10 05:00:00 CEST" "2016-07-10 06:00:00 CEST" #> [71] "2016-07-10 07:00:00 CEST" "2016-07-10 08:00:00 CEST" #> [73] "2016-07-10 09:00:00 CEST" "2016-07-10 10:00:00 CEST" #> [75] "2016-07-10 11:00:00 CEST"
span_around(coffee$time_stamp, "hour", end_shift = "2 hour")
#> [1] "2016-07-07 09:00:00 CEST" "2016-07-07 10:00:00 CEST" #> [3] "2016-07-07 11:00:00 CEST" "2016-07-07 12:00:00 CEST" #> [5] "2016-07-07 13:00:00 CEST" "2016-07-07 14:00:00 CEST" #> [7] "2016-07-07 15:00:00 CEST" "2016-07-07 16:00:00 CEST" #> [9] "2016-07-07 17:00:00 CEST" "2016-07-07 18:00:00 CEST" #> [11] "2016-07-07 19:00:00 CEST" "2016-07-07 20:00:00 CEST" #> [13] "2016-07-07 21:00:00 CEST" "2016-07-07 22:00:00 CEST" #> [15] "2016-07-07 23:00:00 CEST" "2016-07-08 00:00:00 CEST" #> [17] "2016-07-08 01:00:00 CEST" "2016-07-08 02:00:00 CEST" #> [19] "2016-07-08 03:00:00 CEST" "2016-07-08 04:00:00 CEST" #> [21] "2016-07-08 05:00:00 CEST" "2016-07-08 06:00:00 CEST" #> [23] "2016-07-08 07:00:00 CEST" "2016-07-08 08:00:00 CEST" #> [25] "2016-07-08 09:00:00 CEST" "2016-07-08 10:00:00 CEST" #> [27] "2016-07-08 11:00:00 CEST" "2016-07-08 12:00:00 CEST" #> [29] "2016-07-08 13:00:00 CEST" "2016-07-08 14:00:00 CEST" #> [31] "2016-07-08 15:00:00 CEST" "2016-07-08 16:00:00 CEST" #> [33] "2016-07-08 17:00:00 CEST" "2016-07-08 18:00:00 CEST" #> [35] "2016-07-08 19:00:00 CEST" "2016-07-08 20:00:00 CEST" #> [37] "2016-07-08 21:00:00 CEST" "2016-07-08 22:00:00 CEST" #> [39] "2016-07-08 23:00:00 CEST" "2016-07-09 00:00:00 CEST" #> [41] "2016-07-09 01:00:00 CEST" "2016-07-09 02:00:00 CEST" #> [43] "2016-07-09 03:00:00 CEST" "2016-07-09 04:00:00 CEST" #> [45] "2016-07-09 05:00:00 CEST" "2016-07-09 06:00:00 CEST" #> [47] "2016-07-09 07:00:00 CEST" "2016-07-09 08:00:00 CEST" #> [49] "2016-07-09 09:00:00 CEST" "2016-07-09 10:00:00 CEST" #> [51] "2016-07-09 11:00:00 CEST" "2016-07-09 12:00:00 CEST" #> [53] "2016-07-09 13:00:00 CEST" "2016-07-09 14:00:00 CEST" #> [55] "2016-07-09 15:00:00 CEST" "2016-07-09 16:00:00 CEST" #> [57] "2016-07-09 17:00:00 CEST" "2016-07-09 18:00:00 CEST" #> [59] "2016-07-09 19:00:00 CEST" "2016-07-09 20:00:00 CEST" #> [61] "2016-07-09 21:00:00 CEST" "2016-07-09 22:00:00 CEST" #> [63] "2016-07-09 23:00:00 CEST" "2016-07-10 00:00:00 CEST" #> [65] "2016-07-10 01:00:00 CEST" "2016-07-10 02:00:00 CEST" #> [67] "2016-07-10 03:00:00 CEST" "2016-07-10 04:00:00 CEST" #> [69] "2016-07-10 05:00:00 CEST" "2016-07-10 06:00:00 CEST" #> [71] "2016-07-10 07:00:00 CEST" "2016-07-10 08:00:00 CEST" #> [73] "2016-07-10 09:00:00 CEST" "2016-07-10 10:00:00 CEST" #> [75] "2016-07-10 11:00:00 CEST" "2016-07-10 12:00:00 CEST" #> [77] "2016-07-10 13:00:00 CEST"
span_around(coffee$time_stamp, "2 day")
#> [1] "2016-07-07" "2016-07-09" "2016-07-11"
span_around(coffee$time_stamp, "2 day", start_shift = "2 day")
#> [1] "2016-07-05" "2016-07-07" "2016-07-09" "2016-07-11" "2016-07-13"
span_around(emergency$time_stamp, "week")
#> [1] "2015-12-06" "2015-12-13" "2015-12-20" "2015-12-27" "2016-01-03" #> [6] "2016-01-10" "2016-01-17" "2016-01-24" "2016-01-31" "2016-02-07" #> [11] "2016-02-14" "2016-02-21" "2016-02-28" "2016-03-06" "2016-03-13" #> [16] "2016-03-20" "2016-03-27" "2016-04-03" "2016-04-10" "2016-04-17" #> [21] "2016-04-24" "2016-05-01" "2016-05-08" "2016-05-15" "2016-05-22" #> [26] "2016-05-29" "2016-06-05" "2016-06-12" "2016-06-19" "2016-06-26" #> [31] "2016-07-03" "2016-07-10" "2016-07-17" "2016-07-24" "2016-07-31" #> [36] "2016-08-07" "2016-08-14" "2016-08-21" "2016-08-28" "2016-09-04" #> [41] "2016-09-11" "2016-09-18" "2016-09-25" "2016-10-02" "2016-10-09" #> [46] "2016-10-16" "2016-10-23"
span_around(emergency$time_stamp, "2 month")
#> [1] "2015-12-01" "2016-02-01" "2016-04-01" "2016-06-01" "2016-08-01" #> [6] "2016-10-01" "2016-12-01"