Skip to contents

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.

Value

The subset of x that matches the time window.

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.

Author

Corwin Joy

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
#>                  [,1]        [,2]
#> 2003-01-01  0.6263098 -0.08382568
#> 2003-01-03 -0.5072482  1.36770667
#> 2003-01-05  0.2703613 -0.62743462
#> 2003-01-07  0.4674769 -0.21662915
#> 2003-02-09  0.7239950 -0.68371382
#> 2003-02-11  0.6138369 -0.44470273
#> 2003-02-13 -0.6178692  0.60648981
#> 2003-03-15  0.2207249  0.62418307
#> 2003-03-17  1.1279266 -0.69543107
#> 2003-04-19  1.8134543 -0.78363908

window(x, start = "2003-02-01", end = "2003-03-01")
#>                  [,1]       [,2]
#> 2003-02-09  0.7239950 -0.6837138
#> 2003-02-11  0.6138369 -0.4447027
#> 2003-02-13 -0.6178692  0.6064898
window(x, start = as.Date("2003-02-01"), end = as.Date("2003-03-01"))
#>                  [,1]       [,2]
#> 2003-02-09  0.7239950 -0.6837138
#> 2003-02-11  0.6138369 -0.4447027
#> 2003-02-13 -0.6178692  0.6064898
window(x, index. = x.date[1:6], start = as.Date("2003-02-01"))
#>                 [,1]       [,2]
#> 2003-02-09 0.7239950 -0.6837138
#> 2003-02-11 0.6138369 -0.4447027
window(x, index. = x.date[c(4, 8, 10)])
#>                 [,1]       [,2]
#> 2003-01-07 0.4674769 -0.2166291
#> 2003-03-15 0.2207249  0.6241831
#> 2003-04-19 1.8134543 -0.7836391

## Assign to subset
window(x, index. = x.date[c(4, 8, 10)]) <- matrix(1:6, ncol = 2)
x
#>                  [,1]        [,2]
#> 2003-01-01  0.6263098 -0.08382568
#> 2003-01-03 -0.5072482  1.36770667
#> 2003-01-05  0.2703613 -0.62743462
#> 2003-01-07  1.0000000  4.00000000
#> 2003-02-09  0.7239950 -0.68371382
#> 2003-02-11  0.6138369 -0.44470273
#> 2003-02-13 -0.6178692  0.60648981
#> 2003-03-15  2.0000000  5.00000000
#> 2003-03-17  1.1279266 -0.69543107
#> 2003-04-19  3.0000000  6.00000000