sch_adjust: Adjust a vector of dates

Description Usage Arguments Details Examples

View source: R/sch-adjust.R

Description

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.

Usage

1
sch_adjust(x, schedule, adjustment = days(1))

Arguments

x

[Date]

A vector of dates.

schedule

[schedule / event]

A schedule or event.

adjustment

[Period(1) / integer(1) / function / formula]

An adjustment to make whenever a date falls on an event.

If this is a lubridate period object, such as lubridate::days(), or an integer, then the adjustment is repeatedly applied as x + adjustment until the next non-event date is found.

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 schedule, and should return a Date vector of the same size as the original input containing the adjusted dates. See the functions on the help page for adj_following() for some examples.

Details

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.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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))

# The adjustment could also be backwards
sch_adjust("2019-01-01", on_mday(1), adjustment = -days(1))

# 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))

# ---------------------------------------------------------------------------
# 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)

# `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)

# When the distance is the same, the following date is chosen
sch_adjust("2019-01-15", on_mday(c(14, 15, 16)), adj_nearest)

DavisVaughan/almanac3 documentation built on Oct. 30, 2019, 5:59 a.m.