似乎是" timeSeries"长度为0的总是有两个时间戳,0和1。 长度为0的向量的赋值不会导致错误消息,但时间戳的数量和值不变。 将时间戳分配给自己会导致错误消息:
> library(timeSeries)
> X <- timeSeries( matrix(0:3,2,2) )
> setTime(X) <- timeSequence(as.Date("2015-01-01"),as.Date("2015-01-02"),by=as.difftime(1,units="days"))
> # ---------------------
> # As exspected:
>
> head(X,2)
GMT
SS.1 SS.2
2015-01-01 0 2
2015-01-02 1 3
> getTime(head(X,2))
GMT
[1] [2015-01-01] [2015-01-02]
> nrow(head(X,2))
[1] 2
> length(getTime(head(X,2)))
[1] 2
> # ---------------------
> # As exspected:
>
> head(X,1)
GMT
SS.1 SS.2
2015-01-01 0 2
> getTime(head(X,1))
GMT
[1] [2015-01-01]
> nrow(head(X,1))
[1] 1
> length(getTime(head(X,1)))
[1] 1
> # ---------------------
> # Not as exspected:
>
> head(X,0)
GMT
SS.1 SS.2
> getTime(head(X,0))
[1] 1 0
> nrow(head(X,0))
[1] 0
> length(getTime(head(X,0)))
[1] 2
> #====================================
>
> X0 <- head(X,0)
> # Try to assign a vector of length 0 to time(X0):
> setTime(X0) <- integer(0)
> getTime(X0) # no success, but neither error nor warning
[1] 1 0
> #Try to assign current value of time(X0) to itself:
> setTime(X0) <- getTime(X0)
Error: Initialize timeSeries : length of '@positions' not equal to '@.Data' extent
这是&#34; timeSeries&#34;包装
答案 0 :(得分:0)
来自&#34; timeSeries&#34;的文档。包我们了解到&#34; getTime&#34;是&#34; time&#34;的同义词。 因此我一直在寻找该函数的源代码:
> getMethod("time",signature=c(x="timeSeries"))
Method Definition:
function (x, ...)
.time.timeSeries(x, ...)
<environment: namespace:timeSeries>
Signatures:
x
target "timeSeries"
defined "timeSeries"
所以我们需要另一个函数的源代码,&#34; .time.timeSeries&#34;,它只在&#34; timeSeries&#34;内部使用。包:
> getAnywhere(".time.timeSeries")
A single object matching ‘.time.timeSeries’ was found
It was found in the following places
package:timeSeries
namespace:timeSeries
with value
function (x, ...)
{
if (length(x@positions) > 0)
timeDate(x@positions, zone = "GMT", FinCenter = x@FinCenter)
else seq.int(NROW(x))
}
<environment: namespace:timeSeries>
>
我们在这里看到了这个错误。 &#34; seq.int(NROW(X))&#34;是序列1,...,NROW(x)
以1为步长,如果NROW(x)> = 1,但
,步长为-1,如果NROW(x)<1。
因此,在这种特殊情况下,时间序列的长度为0,&#34;时间&#34;是序列&#34; c(1,0)&#34;。 可以解决这个问题如下:
seq.int(1,NROW(x),length.out=NROW(x))
给出相当正确的值&#34;整数(0)&#34;如果&#34; NROW(x)&#34;是0。