Extract index locations for an xts object that correspond to the last
observation in each period specified by on and k.
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
data(sample_matrix)
endpoints(sample_matrix)
#> [1] 0 30 58 89 119 150 180
endpoints(sample_matrix, "weeks")
#> [1] 0 6 13 20 27 34 41 48 55 62 69 76 83 90 97 104 111 118 125
#> [20] 132 139 146 153 160 167 174 180
### 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, ]
#> [,1]
#> Dec 2017 1
#> Dec 2019 25
#> Jan 2021 38