简体   繁体   中英

Can someone explain how endpoints function works?

I don't understand why I receive an output from endpoints function, whereas I am expecting something else. I wrote a pseudo code, could someone explain the mechanism?

y1<-rep(1,times=750)
y1<-xts(y1, order.by = Sys.Date()-1:750)
endpoints(y1,'days',250)

With this code R returns 0 123 347 484 713 750 . Shouldn't it return 250 500 750 ? Or how would it return those indices? Thanks

Looks like endpoints always generates a point at the end of the year and that k is counted from the beginning of the year on:

y1[endpoints(y1,'days',k=250)]
           [,1]
2019-09-07    1
2019-12-31    1
2020-09-06    1
2020-12-31    1
2021-02-06    1

Above result is either 250th day of year or end of year.
Didn't find an explicit mention to this in documentation, but source code shows that year plays a key role even if counting days :


days = {
    ixyday <- posixltindex$year * 1000L + 1900000L + posixltindex$yday
    .Call("endpoints", ixyday, 1L, k, addlast, PACKAGE = "xts")
  }

To get the indexes you are looking for, you could use %% (modulo operator):

which(1:750%%250==0)
[1] 250 500 750

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM