add: Add numerics to mondates using day-of-month logic

View source: R/mondate.r

addR Documentation

Add numerics to mondates using day-of-month logic

Description

Add numerics to mondates. When units = "months", "years", or "quarters" and the numeric is a whole number the result has the same day of the month subject to the number of days in the month, thus abandoning mondate's approach of representing days as fractional months. See also argument 'forcelastday'.

Usage

add(e1, e2, units, forcelastday = FALSE)

Arguments

e1

a mondate

e2

anumeric

units

Anything allowed by base:::as.difftime. In addition, can be "months" or "years". If missing, defaults to timeunits(e1).

forcelastday

If FALSE, the result will have the same day of the month subject to the number of days in the month. If TRUE, the day of the month of the result will be the last day of the month if e1 is on the last day of the month.

Value

A mondate.

Author(s)

Dan Murphy.

Examples

x <- mondate(0:12)
add(x, 1)             # The third date will the the 29th of March
x <- mondate.ymd(2013, 1:11, 15)
add(x, 1)             # Always the 15th of the month. Compare to ...
x + 1
stopifnot(add(x, 13, units = "months") == mondate.ymd(2014, 2:12, 15))
# Last day of February, not a leap year
x <- mondate("2-28-2014")
day(add(x, 1:4, units = "years")) # always the 28th of the month, vs. ...
day(add(x, 1:4, units = "years", forcelastday = TRUE)) # Feb. 29th in 2016

chiefmurph/mondate documentation built on Aug. 29, 2022, 4:13 p.m.