R: Extract time windows from an 'xts' series

window.xts {xts} | R Documentation

## Extract time windows from an `xts` series

### 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 object.
`index.` | a user defined time index. This defaults to the `xts` index for the series via `.index(x)`. When supplied, this is typically a subset of the dates in the full series. The `index.` must be a set of dates that are convertible to `POSIXct`. If you want fast lookups, then `index.` should be sorted and of class `POSIXct`. If an unsorted `index.` is passed in, `window` will sort it.
`start` | a start time. Extract `xts` rows where `index. >= start`. `start` may be any class that is convertible to `POSIXct` such as a character variable in the format ‘YYYY-MM-DD’. If `start` is `NULL` then all `index.` dates are matched.
`end` | an end time. Extract `xts` rows where `index. <= end`. `end` must be convertible to `POSIXct`. If `end` is `NULL` then all `index.` dates are matched.
`...` | currently not used.

### Details

The point of having `window` in addition to the regular subset function is to have a fast way of extracting time ranges from an `xts` series. In particular, this method will convert `start` and `end` to `POSIXct` then do a binary lookup on the internal `xts` index to quickly return a range of matching dates. With a user supplied `index.`, a similarly fast invocation of `findInterval` is used so that large sets of sorted dates can be retrieved quickly.

### Value

The matching time window is extracted.

### 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

[Package xts version 0.12.1 Index]