Given a schedule of events, create a new schedule where the events are adjusted according to some rule.

  • roll_by() adjusts the events of a schedule by some incremental period.

  • roll_forward() and roll_backward() adjust the events of a schedule to the nth previous/next occurrence of some other scheduled event.

roll_by(x, n, unit, .p = NULL)

roll_forward(x, to_schedule, n = 1, .p = NULL)

roll_backward(x, to_schedule, n = 1, .p = NULL)

Arguments

x

The schedule of events to adjust.

n

The increment of the event adjustment.

  • For roll_by() this is the increment of the unit period to adjust the events by. Eg. 2 to adjust the events two unit periods into the future or -3 to adjust the events three unit periods into the past.

  • For roll_forward() and roll_backward(), n defaults to 1, indicating the schedule should be adjusted to the to_schedule event immediately preceding/following the events of x. If more than 1, will skip n-1 occurrences.

unit

A character shortcut for a period object. Eg. "year", "years", "month", "months", "week", "weeks" etc. Can be any value accepted by lubridate::period().

.p

Optionally, a schedule to use for limiting the adjustment performed on x. Events falling on .p will be adjusted. Events not falling on .p will be returned unadjusted in the output schedule. Leave NULL (the default) to adjust all the events of x.

  • Eg. roll_forward(x, to_schedule = on_wday("Sun")) rolls the events of x to the next Sunday.

to_schedule

A schedule to which events can be rolled.

Value

A schedule object.

Examples

library(lubridate, warn.conflicts = FALSE) library(magrittr, warn.conflicts = FALSE) # Imagine you get paid on the 25th of the month on_payday <- on_mday(25) schedule_days(on_payday, during = 2000)
#> [1] "2000-01-25" "2000-02-25" "2000-03-25" "2000-04-25" "2000-05-25" #> [6] "2000-06-25" "2000-07-25" "2000-08-25" "2000-09-25" "2000-10-25" #> [11] "2000-11-25" "2000-12-25"
# Except if your payday falls on a weekend, in which case it moves to the # next weekday on_payday %>% roll_forward(to = on_weekday(), .p = on_weekend()) %>% schedule_days(during = 2000)
#> [1] "2000-01-25" "2000-02-25" "2000-03-27" "2000-04-25" "2000-05-25" #> [6] "2000-06-26" "2000-07-25" "2000-08-25" "2000-09-25" "2000-10-25" #> [11] "2000-11-27" "2000-12-25"
# For some people payday may adjust to the previous weekday if it falls on # a weekend on_payday %>% roll_backward(to = on_weekday(), .p = on_weekend()) %>% schedule_days(during = 2000)
#> [1] "2000-01-25" "2000-02-25" "2000-03-24" "2000-04-25" "2000-05-25" #> [6] "2000-06-23" "2000-07-25" "2000-08-25" "2000-09-25" "2000-10-25" #> [11] "2000-11-24" "2000-12-25"
# Imagine the garbage truck normally comes every Monday, but if # Monday is a state holiday, then it comes on Tuesday instead. on_labor_day <- on_first(on_wday("Mon"), within_given = "month") %>% only_occur(in_month("Sep")) on_christmas_day <- only_occur(in_month("Dec"), on_mday(25)) on_non_working_day <- on_weekend() %>% also_occur(on_labor_day) %>% also_occur(on_christmas_day) on_my_business_day <- dont_occur(on_non_working_day) on_normal_trash_day <- on_wday("Mon") on_trash_day <- on_normal_trash_day %>% roll_forward(to_schedule = on_my_business_day, .p = on_non_working_day) # A Monday in September happen(on_normal_trash_day, ymd("2019-09-09"))
#> [1] TRUE
happen(on_trash_day, ymd("2019-09-09"))
#> [1] TRUE
# Labor Day Monday should not be trash day happen(on_normal_trash_day, ymd("2019-09-02"))
#> [1] TRUE
happen(on_trash_day, ymd("2019-09-02"))
#> [1] FALSE
# The day after Labor Day Monday is trash day happen(on_normal_trash_day, ymd("2019-09-03"))
#> [1] FALSE
happen(on_trash_day, ymd("2019-09-03"))
#> [1] TRUE
# Say that a trash inspector always comes the day after trash day, whatever # day that happens to be. on_inspection_day <- on_trash_day %>% roll_by(1, "day") # Inspector comes on a Tuesday in September happen(on_inspection_day, ymd("2019-09-10"))
#> [1] TRUE
# Inspector doesn't come on that Wednesday happen(on_inspection_day, ymd("2019-09-11"))
#> [1] FALSE
# Inspector doesn't come on the Tuesday after Labor Day Monday happen(on_inspection_day, ymd("2019-09-03"))
#> [1] FALSE
# Inspector does come on that Wednesday happen(on_inspection_day, ymd("2019-09-04"))
#> [1] TRUE