Learn R Programming

xts (version 0.14.1)

endpoints: Locate Endpoints by Time

Description

Extract index locations for an xts object that correspond to the last observation in each period specified by on and k.

Usage

endpoints(x, on = "months", k = 1)

Value

A numeric vector of beginning with 0 and ending with the number of of observations in x.

Arguments

x

An xts object.

on

A character string specifying the period.

k

The number of periods each endpoint should cover.

Author

Jeffrey A. Ryan

Details

endpoints() returns a numeric vector that always begins with zero and ends with the number of observations in x.

Periods are always based on the distance from the UNIX epoch (midnight 1970-01-01 UTC), not the first observation in x. See the examples.

Valid values for the on argument are: “us” (microseconds), “microseconds”, “ms” (milliseconds), “milliseconds”, “secs” (seconds), “seconds”, “mins” (minutes), “minutes”, “hours”, “days”, “weeks”, “months”, “quarters”, and “years”.

Examples

Run this code

data(sample_matrix)

endpoints(sample_matrix)
endpoints(sample_matrix, "weeks")

### example of how periods are based on the UNIX epoch,
### *not* the first observation of the data series
x <- xts(1:38, yearmon(seq(2018 - 1/12, 2021, 1/12)))
# endpoints for the end of every other year
ep <- endpoints(x, "years", k = 2)
# Dec-2017 is the end of the *first* year in the data. But when you start from
# Jan-1970 and use every second year end as your endpoints, the endpoints are
# always December of every odd year.
x[ep, ]

Run the code above in your browser using DataLab