Learn R Programming

TimeWarp (version 1.0.15)

dateMatch: Match Dates in a Table

Description

Return the indices of dates in a table that match, according to rules "before", "after", etc. dateMatch() is a generic, with methods for character, Date, POSIXct, and POSIXlt.

Usage

dateMatch(x, table, how=c("NA", "before", "after", "nearest", "interp"),
          error.how=c("NA", "drop", "nearest", "stop"),
          nomatch=NA, offset=NULL, value=FALSE, optimize.dups=TRUE)

Arguments

x

A "Date" vector, or a character vector that can be converted to "Date"

table

A "Date" vector, or a character vector that can be converted to "Date". Must be strictly increasing.

how

A character string. Determines how values in x that do not have exact matches in table are handled.

"before"

the element in table that is just before

"after"

the element in table that is just after

"nearest"

the element in table that is nearest

"interp"

an interpolated index

"NA"

return the nomatch value

For convenience, how can specify both how and error.how separated with a period, e.g., how="before.nearest" is equivalent to how="before" and error.how="nearest"

error.how

A character string. Determines how to handle values in x that do not have exact matches in table and for which the how rule fails (e.g. when how is one of "before", "after", or "interp").

"NA"

return the "nomatch" value

"drop"

causes non-matched values to be dropped

"nearest"

pick the nearest value in table.

"stop"

stop with an error.

See the note on argument how for another way of specifying error.how. A value for error.how is ignored if the value for how has a period in it.

nomatch

The value to return for nomatch cases. If value=TRUE, then nomatch must be a Date value, Otherwise it must be a numeric value. NA is the default.

offset

If an integer, this offset is added to the computed indices after matching. (Can be an integer value represented as a float.) Non-integer and non-numeric values cause an error. It is possible that later on, character values may be allowed to specify a computed offset to the values in x (e.g., something like "+1 bizdays@NYSEC"). If the result is outside the range of indices of table, NA is returned in those positions.

value

If TRUE, the matching value in table is returned instead of the index.

optimize.dups

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

Value

The indices of the matches for the elements of x in table, or the actual matching values from table if value==TRUE. In the latter case, 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.

Details

Uses match and findInterval to perform matching.

Examples

Run this code
# NOT RUN {
d1 <- dateParse(c("2001/01/10", "2001/03/12"))
d2 <- dateSeq(dateParse("2001/01/01"), by = "weeks", len = 20)
dateMatch(d1, dateParse(), how = "nearest", error.how = "drop")
dateMatch(d1, dateParse(), how = "nearest", error.how = "stop")
dateMatch(d1, dateParse(), how = "nearest.stop")
dateMatch(d1, d2, how = "after")
dateMatch(d1, d2, how = "after", offset = -3)
dateMatch(dateParse(c("2001/01/10", "2001/01/17", "2001/03/12")),
dateSeq(dateParse("2001/01/01"), by = "weeks", len = 20), how = "after",
offset = 10, value = TRUE)
# }

Run the code above in your browser using DataLab