Search code examples
rxtsendpoints

Why does xts' endpoints() ignores the data on the right boundary of the interval?


I have the following data in the variable t, generated by t <- xts(1:6, order.by=seq(as.POSIXct("2000-01-01 00:01:00", tz="GMT"), length.out = 6, by="1 min"))

                    [,1]
2000-01-01 00:01:00    1
2000-01-01 00:02:00    2
2000-01-01 00:03:00    3
2000-01-01 00:04:00    4
2000-01-01 00:05:00    5
2000-01-01 00:06:00    6

I would expect that endpoints(t, on="mins", k=3) gives me c(0, 3, 6) which correspondes to 2000-01-01 00:03:00, 2000-01-01 00:06:00.

Instead, it returns c(0,2,5,6). What is the logic behind this behavior?

Then what is the reason to have the leading zero? It is written in the documentation that it is there but why?


Solution

  • The premise of your question is incorrect. endpoints() does not ignore data on the interval's right-bound. Your example data only include values on the left-bound; i.e. all the observations in your data occur at the beginning of each minute. If they were indeed the right-bound, that would imply "2000-01-01 00:00:00.000" is the last observed value in 1999.

    The first element needs to be zero only because of how the output of endpoints() is used in other functions. Hypothetically, the code could probably be refactored to remove the requirement that the first element be zero. I say hypothetically, because that refactor could potentially break other code, which makes me reluctant to do it.