roll_by | R Documentation |
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)
x |
The schedule of events to adjust. |
n |
The increment of the event adjustment.
|
unit |
A character shortcut for a period object. Eg. "year", "years",
"month", "months", "week", "weeks" etc. Can be any value accepted by
|
.p |
Optionally, a schedule to use for limiting the adjustment
performed on
|
to_schedule |
A schedule to which events can be rolled. |
A schedule object.
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)
# 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)
# 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)
# 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"))
happen(on_trash_day, ymd("2019-09-09"))
# Labor Day Monday should not be trash day
happen(on_normal_trash_day, ymd("2019-09-02"))
happen(on_trash_day, ymd("2019-09-02"))
# The day after Labor Day Monday is trash day
happen(on_normal_trash_day, ymd("2019-09-03"))
happen(on_trash_day, ymd("2019-09-03"))
# 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"))
# Inspector doesn't come on that Wednesday
happen(on_inspection_day, ymd("2019-09-11"))
# Inspector doesn't come on the Tuesday after Labor Day Monday
happen(on_inspection_day, ymd("2019-09-03"))
# Inspector does come on that Wednesday
happen(on_inspection_day, ymd("2019-09-04"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.