dateWarp: Date vector shifting and alignment

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

View source: R/dateWarp.R

Description

Perform multiple shifts and alignments on Date vectors. dateWarp() is a generic, with methods for character, factor, Date, POSIXct, and POSIXlt.

Usage

1
2
3
4
dateWarp(date, spec, holidays = NULL, by = NULL, direction = 1,
         duplicates.keep = TRUE, optimize.dups=TRUE)
dateWarpAppend(date, ..., where = c("sorted", "start", "end"), empty.ok = FALSE,
               duplicates.ok = FALSE)

Arguments

date

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

spec

a specification of shifts and alignment tranformations. See ‘Details’.

holidays

a character string naming the holiday series (see holidays).

by

how to warp. Can be one of "days", "bizdays", "weeks", "months", or "years". "bizdays" can contain a holiday specification like: "bizdays@NYSEC"

direction

which direction to warp. a numeric value of 1 for the future, and -1 for the past (be careful about using variables for this value, for if it's value is negative and you place a minus sign in front of the variable, you'll go back to the future).

duplicates.keep

logical indicating whether or not to keep duplicate dates.

...

arguments to pass to dateWarp.

where

character string. can be "sorted" to sort the result instead of append, "start" to prepend, or "end" to append.

empty.ok

is it okay for the dates argument to be empty?

duplicates.ok

logical indicating whether or not to keep duplicate dates.

optimize.dups

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

Details

spec specify tranformations in several ways:

Value

A date vector that is a transformed version of the input dates. Multiple shift or alignment transformations can be specified. If more that one is given, each vill be applied in turn to the result of the previous one. The class of the returned value is the same as the class of date for character, Date, POSIXct, and POSIXlt. For date of other classes, the class of the returned value is Date, but this may change in the future.

Author(s)

Lars Hansen, Tony Plate

See Also

dateAlign, dateShift, dateMatch, dateParse, and dateSeq

The Holidays package contains holidays data, which is registered with the TimeWarp package when the Holidays package is loaded.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
library(Holidays)
dates <- dateSeq("2001/12/20", by = 'bizdays', len = 9, holidays = "NYSEC")
dateWarp(dates, -1:1, by = "bizdays", holidays = "NYSEC", duplicates.keep = FALSE)
dateWarp(dates, "+1 bizdays@NYSEC")
dateWarp(dates, list(0:6), by = "bizdays@NYSEC")
dateWarp(dates, list(-1:1, unique = TRUE), by = "bizdays")
dateWarp(dates, list(latest = "2001/12/25"))
x <- dateSeq("2001/01/01", len = 4, by = "weeks")
dateWarp(dates, list(align = list(to = x, how = "after")))
dateWarp(dates, list(shift = list(by = "bizdays", k.by = 2, direction = 1, holidays = "NYSEC"),
                     shift = 2),
         by = "days", direction = -1,
holidays = "NONE")
dateWarp(dates, hol = "NYSEC",
         list(shift = list(k.by = 1, by = "months"),
         shift=list(k.by = -1, by = "bizdays")))
dateWarp(dates, list(align = list(by = "bizdays"),
                     shift = list(by = "months", k.by = 2), holidays = "JPNEX"))

# Options expirations dates in 2008
(d1 <- dateSeq('2008-01-01', len=12, by='months'))
(d2 <- dateAlign(d1, by='months', dir=-1))
(d3 <- dateAlign(d2, by='weeks', week.align=5))
(d4 <- dateWarp(d3, 14, by='days'))
(d5 <- dateAlign(d4, by='bizdays@NYSEC', dir=-1))
# Version that uses a list 'spec' to dateWarp
dateWarp(d1, list(align=list(by='months', dir=-1),
                  align=list(by='weeks', week.align=5),
                  shift=list(by='days', 14),
                  align=list(by='bizdays@NYSEC', dir=-1)))
# In 2008 the March options expiration is a Thursday because Friday was a holiday
dateDow(d5)

TimeWarp documentation built on May 2, 2019, 4:48 p.m.