window.xts: Extract Time Windows from xts Objects

View source: R/xts.methods.R

window.xtsR Documentation

Extract Time Windows from xts Objects


Method for extracting time windows from xts objects.


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



An xts object.


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


A start time coercible to POSIXct.


An end time coercible to POSIXct.




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.


The subset of x that matches the time window.


Corwin Joy

See Also

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


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

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. =[1:6], start = as.Date("2003-02-01"))
window(x, index. =[c(4, 8, 10)])

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

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