TimeWarp (version 1.0.15)

dateAlign: Date alignment

Description

Align a date vector the a day, bizday, month, week or year boundary. dateAlign() is a generic, with methods for character, Date, POSIXct, and POSIXlt.

Usage

dateAlign(x, by = "days", k.by = 1, direction = 1,
          week.align = NULL, 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 time period. Can be one of "days", "bizdays","weeks", "months", or "years".

k.by

positive integer giving the number of the by units to align to. Ignored for "bizdays" and "weeks".

direction

integer with either -1 or 1, to align to the previous or next time that is an integer number of the k.by * by units.

week.align

if not NULL, and by is "weeks", an integer, 0 to 6 with 0 being Sunday, to specify a weekday to align to.

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 whose elements are moved up or down (according to direction) so that they lie on integer multiples of k.by * by units of time starting from the beginning of the next bigger time unit (e.g. if by = "days", then align to multiples of k.by days added to the first of the month. Note that for "weeks", and "bizdays", k.by is assumed to be 1 and ignored; "weeks" without week.align is equivalent to "days". Also note that k.by should be a divisor of the number of by units in the next larger time unit, or NA values will result. 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

dateShift, dateWarp, dateMatch, dateParse, dateSeq

Examples

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

Run the code above in your browser using DataCamp Workspace