[英]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.我不明白为什么我会从端点 function 收到 output,而我期待别的东西。 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
.使用此代码 R 返回
0 123 347 484 713 750
。 Shouldn't it return 250 500 750
?它不应该返回
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:看起来
endpoints
总是在年底生成一个点,并且k
从年初开始计算:
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.以上结果是一年中的第 250 天或年末。
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
数, year
也起着关键作用:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.