dateAlign: Date alignment

Description Usage Arguments Value Note Author(s) See Also Examples

View source: R/dateAlign.R

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

1
2
3
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.

Note

Alignment of dates can be thought of as a partition on date sequences where an input date is aligned to the first date in a partition, if it is not already aligned. The direction of alignment determines which partition to use for the alignment. If the direction is -1 then alignment happens in the partition which the date falls in. If +1 then alignment happens in the partition just after the partition in which the dates falls.

Author(s)

Lars Hansen, Tony Plate

See Also

dateShift, dateWarp, dateMatch, dateParse, dateSeq

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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)

Example output

[1] "2007-12-05"
 [1] "2008-12-27" "2008-12-28" "2008-12-29" "2008-12-30" "2008-12-31"
 [6] "2009-01-01" "2009-01-02" "2009-01-03" "2009-01-04" "2009-01-05"
[11] "2009-01-06"
 [1] "2008-12-25" "2008-12-28" "2008-12-28" "2008-12-28" "2008-12-31"
 [6] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-04" "2009-01-04"
[11] "2009-01-04"
 [1] "2008-12-29" "2008-12-29" "2008-12-29" "2008-12-30" "2008-12-31"
 [6] "2009-01-01" "2009-01-02" "2009-01-05" "2009-01-05" "2009-01-05"
[11] "2009-01-06"
 [1] "2008-12-29" "2008-12-29" "2008-12-29" "2008-12-30" "2008-12-31"
 [6] "2009-01-02" "2009-01-02" "2009-01-05" "2009-01-05" "2009-01-05"
[11] "2009-01-06"
 [1] "2008-11-01" "2008-11-01" "2008-11-01" "2008-11-01" "2008-11-01"
 [6] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
[11] "2009-01-01"
 [1] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
 [6] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
[11] "2007-01-01"

TimeWarp documentation built on May 2, 2019, 11:11 a.m.