holiday-utilities {almanac}R Documentation

Holiday utility functions

Description

These three functions allow you to tweak existing holidays created by rholiday() so that they more properly align with business calendars. The resulting holidays can then be added into an rcalendar().

Usage

hol_observe(x, adjust_on, adjustment)

hol_offset(x, by)

hol_rename(x, name)

Arguments

x

⁠[rholiday]⁠

An rholiday.

adjust_on

⁠[rschedule]⁠

An rschedule that determines when the adjustment is to be applied.

adjustment

⁠[function]⁠

An adjustment function to apply to problematic dates. Typically one of the pre-existing adjustment functions, like adj_nearest().

A custom adjustment function must have two arguments x and rschedule. x is the complete vector of dates that possibly need adjustment. rschedule is the rschedule who's event set determines when an adjustment needs to be applied. The function should adjust x as required and return the adjusted Date vector.

by

⁠[integer(1)]⁠

A single integer to offset by.

name

⁠[character(1)]⁠

A new name for the holiday.

Examples


on_weekends <- weekly() %>%
  recur_on_weekends()

# Christmas, adjusted to nearest Friday or Monday if it falls on a weekend
on_christmas <- hol_christmas() %>%
  hol_observe(on_weekends, adj_nearest)

# Boxing Day is the day after Christmas.
# If observed Christmas is a Friday, then observed Boxing Day should be Monday.
# If observed Christmas is a Monday, then observed Boxing Day should be Tuesday.
on_boxing_day <- on_christmas %>%
  hol_offset(1) %>%
  hol_observe(on_weekends, adj_following) %>%
  hol_rename("Boxing Day")

christmas_dates <- alma_events(on_christmas, year = 2010:2015)
boxing_day_dates <- alma_events(on_boxing_day, year = 2010:2015)

data.frame(
  christmas = christmas_dates,
  boxing_day = boxing_day_dates,
  christmas_weekday = lubridate::wday(christmas_dates, label = TRUE),
  boxing_day_weekday = lubridate::wday(boxing_day_dates, label = TRUE)
)

[Package almanac version 1.0.0 Index]