window.xts: Extract Time Windows from xts Objects

View source: R/xts.methods.R

window.xtsR Documentation

Extract Time Windows from xts Objects

Description

Method for extracting time windows from xts objects.

Usage

## S3 method for class 'xts'
window(x, index. = NULL, start = NULL, end = NULL, ...)

Arguments

x

An xts object.

index.

A user defined time index (default .index(x)).

start

A start time coercible to POSIXct.

end

An end time coercible to POSIXct.

...

Unused.

Details

The xts window() method provides an efficient way to subset an xts object between a start and end date using a binary search algorithm. Specifically, it converts start and end to POSIXct and then does a binary search of the index to quickly return a subset of x between start and end.

Both start and end may be any class that is convertible to POSIXct, such as a character string in the format ‘yyyy-mm-dd’. When start = NULL the returned subset will begin at the first value of index.. When end = NULL the returned subset will end with the last value of index.. Otherwise the subset will contain all timestamps where index. is between start and end, inclusive.

When index. is specified, findInterval() is used to quickly retrieve large sets of sorted timestamps. For the best performance, index. must be a sorted POSIXct vector or a numeric vector of seconds since the epoch. index. is typically a subset of the timestamps in x.

Value

The subset of x that matches the time window.

Author(s)

Corwin Joy

See Also

subset.xts(), findInterval(), xts()

Examples


## xts example
x.date <- as.Date(paste(2003, rep(1:4, 4:1), seq(1,19,2), sep = "-"))
x <- xts(matrix(rnorm(20), ncol = 2), x.date)
x

window(x, start = "2003-02-01", end = "2003-03-01")
window(x, start = as.Date("2003-02-01"), end = as.Date("2003-03-01"))
window(x, index. = x.date[1:6], start = as.Date("2003-02-01"))
window(x, index. = x.date[c(4, 8, 10)])

## Assign to subset
window(x, index. = x.date[c(4, 8, 10)]) <- matrix(1:6, ncol = 2)
x


xts documentation built on June 22, 2024, 9:56 a.m.