lastWeekday: Functions for Computing Days of the Week

last_weekdayR Documentation

Functions for Computing Days of the Week

Description

Functions for computing a specifed day-of-week, such as ‘the last Friday of October 2015’.

Usage

last_weekday(weekday, x, shift = 0L,
             period = "month", before, inclusive = TRUE)
nth_weekday(weekday, x, n = 1L)

Arguments

x

a vector of class Date (but only the YYYY-MM part is relevant)

shift

a vector of integers

weekday

an integer (1 is Monday, 2 is Tuesday, and so on)

period

character. Currently ignored.

before

a Date. See also inclusive.

inclusive

logical. Is before meant is ‘before but including’?

n

an integer

Details

last_weekday computes the last day-of-the-week (specified as an integer 0 to 6, with Sunday being 0) in a given month, e.g. ‘the last Friday’. shift moves forward (when positive) or backward (when negative) by one week; see Examples.

nth_weekday gives the n-th day-of-the-week (specified as an integer 0 to 6, with Sunday being 0) of a given month, e.g. ‘the second Monday’.

Value

Date

Author(s)

Enrico Schumann

References

B.D. Ripley and K. Hornik. Date-Time Classes. R-News, 1(2):8–12, 2001.

See Also

DateTimeClasses

Many useful functions are also in package chron.

Examples

## GOAL:     find the third Friday in March 2013
## SOLUTION: find the last Friday in February 2013 and
##           shift forward by 3 weeks
last_weekday(5, as.Date("2013-02-01"), shift = 3)

## ... or much simpler
nth_weekday(5, as.Date("2013-03-01"), 3)


enricoschumann/datetimeutils documentation built on April 2, 2024, 11:10 a.m.