问题描述
似乎长度为 0 的timeSeries"总是有两个时间戳,0 和 1.分配长度为 0 的向量不会导致错误消息,但时间戳的数量和值不变.将时间戳分配给自己会导致错误消息:
It seems that a "timeSeries" of length 0 always has two timestamps, 0 and 1.Assignment of a length 0 vector doesn't cause an error message, but number and values of the timestamps are unchanged.Assignment of the timestamps to themselfs results in an error message:
> 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
这是timeSeries"包中的错误吗?
Is this a bug in the "timeSeries" package?
推荐答案
从timeSeries"包的文档中我们了解到getTime"是time"的同义词.因此我正在寻找该函数的源代码:
From the documentation of the "timeSeries" package we learn that "getTime" is a synonym for "time".Hence I was looking for the source code of that function:
> getMethod("time",signature=c(x="timeSeries"))
Method Definition:
function (x, ...)
.time.timeSeries(x, ...)
<environment: namespace:timeSeries>
Signatures:
x
target "timeSeries"
defined "timeSeries"
所以我们需要另一个函数.time.timeSeries"的源代码,它只在timeSeries"包内部使用:
So we need the source code of another function, ".time.timeSeries", which is only used internally by the "timeSeries" package:
> 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>
>
在这里我们看到了错误."seq.int(NROW(x))" 是序列 1,...,NROW(x)
Here we see the bug. "seq.int(NROW(x))" is the sequence 1,...,NROW(x)
步长为 1,如果 NROW(x)>=1,但是
in steps of 1, if NROW(x)>=1, but
以 -1 为步长,如果 NROW(x)
in steps of -1, if NROW(x)<1.
因此在这种特殊情况下,时间序列的长度为 0,时间"的值是序列c(1,0)".可以通过以下方式解决此问题:
Hence in this particular case, where the length of the time series is 0, the value of "time" is the sequence "c(1,0)".One can fix this as follows:
seq.int(1,NROW(x),length.out=NROW(x))
如果 "NROW(x)" 为 0,则给出相当正确的值 "integer(0)".
gives the rather correct value "integer(0)" if "NROW(x)" is 0.
这篇关于timeSeries 包中长度为 0 的时间序列具有可疑的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!