sch_adjust()
takes an existing vector of dates and shifts it by applying
an adjustment
whenever a date in x
is also an event defined by
the schedule
.
sch_adjust(x, schedule, adjustment = days(1))
x |
A vector of dates. |
---|---|
schedule |
A schedule or event. |
adjustment |
An adjustment to make whenever a date falls on an event. If this is a lubridate period object, such as If this is a function or formula (i.e., a lambda function), then it
should accept 2 arguments, the dates to adjust and the original |
Internally, a period / integer adjustment
is applied repeatedly until the
next non-event date is found. Be careful! This can result in infinite loops
with improperly defined schedules, which are impossible for us to guard
against for you.
A custom adjustment
function should expect to accept the dates
requiring adjustment, and should completely adjust them to the next
non-event date. It is the responsibility of the adjustment
function to
ensure that the date resulting from the adjustment is not also an
event date.
library(lubridate, warn.conflicts = FALSE) # The first of the month is an "event" so we have to adjust # our current date to avoid that. sch_adjust("2019-01-01", on_mday(1))#> [1] "2019-01-02"# The adjustment could also be backwards sch_adjust("2019-01-01", on_mday(1), adjustment = -days(1))#> [1] "2018-12-31"# Period adjustments are applied repeatedly until the next non-event can # be found. Here, 2019-01-01 is an event, so we move to 2019-01-02, but # that is an event too, so we move to 2019-01-03. sch_adjust("2019-01-01", on_mday(1:2))#> [1] "2019-01-03"# --------------------------------------------------------------------------- # Custom adjustments # Financial business logic might require special rules, a few of which are # encoded in the `adj_*()` functions. For example, `adj_modified_following()` # will use an adjustment of `+days(1)`, unless making that adjustment would # place you past the last day in the month, in which case an adjustment of # `-days(1)` is made instead. sch_adjust("2019-01-31", on_mday(31), adj_modified_following)#> [1] "2019-01-30"# `adj_nearest()` looks to the closest non-event date. Here, the 13th # is closer than the 18th, so it is chosen as the adjustment date. sch_adjust("2019-01-15", on_mday(c(14, 15, 16, 17)), adj_nearest)#> [1] "2019-01-13"# When the distance is the same, the following date is chosen sch_adjust("2019-01-15", on_mday(c(14, 15, 16)), adj_nearest)#> [1] "2019-01-17"