Learn R Programming

TimeWarp (version 1.0.15)

dateShift: Date shifting

Description

Shift a date vector a number of days, bizdays, months, weeks or years. dateShift() is a generic, with methods for character, Date, POSIXct, and POSIXlt.

Usage

dateShift(x, by = "days", k.by = 1, direction = 1, holidays = NULL,
          silent = FALSE, optimize.dups=TRUE)

Arguments

x

Date vector, or a character vector that can be converted to Date by dateParse.

by

character string with the time unit of the shifts. Can be one of "days", "bizdays","weeks", "months", or "years".

k.by

positive integer with the number of by time units to shift.

direction

integer with the direction to shift. A value of 1 for the future, and -1 for the past.

holidays

character string naming the holiday series (see holidays).

silent

logical indicating whether or not to suppress warnings about arguments.

optimize.dups

If TRUE, internally optimize by not performing the same computation multiple times for duplicates. This does not change the return value.

Value

Date vector that is a time shifted version of the input dates. If shifting by "bizdays", weekends and holidays will be skipped. The class of the returned value is the same as the class of x for character, Date, POSIXct, and POSIXlt. For x of other classes, the class of the returned value is Date, but this may change in the future.

See Also

dateAlign, dateWarp, dateMatch, dateParse, dateSeq

Examples

Run this code
# NOT RUN {
dateShift("2007/12/06", by = "days", k.by = 7, direction = -1)
date <- as.Date("2009/1/1") + -5:5
dateShift(date, by = "days", silent = TRUE)
library(Holidays)
dateShift(date, by = "bizdays", k.by = 5, holidays = "NYSEC")
dateShift(date, by = "weeks", k.by = 2)
dateShift(date, by = "months", k.by = "3", direction = "-1")
dateShift(date, by = "years", k.by = 1, direction = 1)
# }

Run the code above in your browser using DataLab