Learn R Programming

xts (version 0.13.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.

Usage

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

Value

A numeric vector of beginning with 0 and ending with the value equal to the number of observations in the x argument.

Arguments

x

an xts object

on

the periods endpoints to find as a character string

k

along every k-th element - see notes

Author

Jeffrey A. Ryan

Details

endpoints returns a numeric vector corresponding to the last observation in each period. The vector always begins with zero and ends with the last observation in x.

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

Valid values for the argument on include: “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