nth_day: Compute Reference Dates

View source: R/functions.R

nth_dayR Documentation

Compute Reference Dates

Description

Compute sequences of reference dates, such as last day of month or first day of quarter.

Usage

nth_day(timestamps, period = "month", n,
        start, end, business.days = FALSE,
        missing = "previous", index = FALSE)

Arguments

timestamps

timestamps: a sorted vector of Dates

period

numeric or character: supported are "week", "month", "quarter", "halfyear", "year". If numeric, period is interpreted as a month number, with January being 1. Also possible are month names, either English as in month.name or month.abb, or as defined in the current locale (see strftime format specification "%b" and "%B").

n

numeric or character: currently supported are "first" and "last". If numeric, it will be interpreted as the n-th day of the period.

start

Date

end

Date

business.days

logical

missing

character. Not supported yet.

index

logical. If TRUE, the indices (instead of actual timestamps) are returned.

Details

The function computes sequences of dates that are often used as reference dates, for instance in financial reporting: last day of the month or of the year, or a particular day of the month.

The function takes a vector of timestamps and returns a subset of these timestamps. Alternatively, a sequence of calendar days may be constructed by specifying start and end.

Value

A vector of timestamps or, if index is TRUE, a vector of integers.

Author(s)

Enrico Schumann

See Also

nth_weekday

Examples

timestamps <- seq(from = as.Date("2001-01-01"),
                  to   = as.Date("2001-04-15"),
                  by   = "1 day")

nth_day(timestamps, period = "quarter", n = "last")
## [1] "2001-03-31" "2001-04-15"

nth_day(timestamps, period = "quarter", n = 10)
## [1] "2001-01-10" "2001-04-10"

nth_day(timestamps, period = "quarter", n = 1:2)
## [1] "2001-01-01" "2001-01-02" "2001-04-01" "2001-04-02"

nth_day(timestamps, period = "month", n = "last")
## [1] "2001-01-31" "2001-02-28" "2001-03-31" "2001-04-15"


nth_day(start = as.Date("2016-06-03"),
        end   = as.Date("2017-08-01"),
        period = c(6, 12), n = 3)
## [1] "2016-06-05" "2016-12-03" "2017-06-03"

nth_day(start = as.Date("2016-06-03"),
        end   = as.Date("2017-08-01"),
        period = c("Jun", "Dec"), n = c(3, 5))
## [1] "2016-06-05" "2016-06-07" "2016-12-03" "2016-12-05"
## [5] "2017-06-03" "2017-06-05"


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