sub_set: Subset an _xts_ time series (extract an _xts_ sub-series...

Description Usage Arguments Details Value Examples

View source: R/rutils.R

Description

Subset an xts time series (extract an xts sub-series corresponding to the input dates).

Usage

1
sub_set(x_ts, start_date, end_date, get_rows = TRUE)

Arguments

x_ts

An xts time series.

start_date

The start date of the extracted time series data.

end_date

The end date of the extracted time series data, or the number of data rows to be extracted.

get_rows

A Boolean argument: if TRUE then extract the given number of rows of data, else extract the given number of calendar days (default is TRUE).

Details

The function sub_set() extracts an xts sub-series corresponding to the input dates. If end_date is a date object or a character string representing a date, then sub_set() performs standard bracket subsetting using the package xts.

The rows of data don't necessarily correspond to consecutive calendar days because of weekends and holidays. For example, 10 consecutive rows of data may correspond to 12 calendar days. So if end_date is a number, then we must choose to extract either the given number of rows of data (get_rows=TRUE) or the given number of calendar days (get_rows=FALSE).

If end_date is a positive number then sub_set() returns the specified number of data rows from the future, and if it's negative then it returns the data rows from the past.

If end_date is a number, and either start_date or end_date are outside the date range of x_ts, then sub_set() extracts the maximum available range of x_ts.

Value

An xts time series with the same number of columns as the input time series.

Examples

1
2
3
4
5
6
7
8
# Subset an xts time series using two dates
rutils::sub_set(rutils::etf_env$VTI, start_date="2015-01-01", end_date="2015-01-10")
# Extract 6 consecutive rows of data from the past, using a date and a negative number
rutils::sub_set(rutils::etf_env$VTI, start_date="2015-01-01", end_date=-6)
# Extract 6 calendar days of data
rutils::sub_set(rutils::etf_env$VTI, start_date="2015-01-01", end_date=6, get_rows=FALSE)
# Extract up to 100 consecutive rows of data
rutils::sub_set(rutils::etf_env$VTI, start_date="2016-08-01", end_date=100)

algoquant/rutils documentation built on Feb. 26, 2021, 1:30 a.m.