r - Time series of length 0 from the timeSeries package has dubious timestamps -
it seems "timeseries" of length 0 has 2 timestamps, 0 , 1. assignment of length 0 vector doesn't cause error message, number , values of timestamps unchanged. assignment of timestamps themselfs results in 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")) > # --------------------- > # 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 > # --------------------- > # 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 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 assign vector of length 0 time(x0): > settime(x0) <- integer(0) > gettime(x0) # no success, neither error nor warning [1] 1 0 > #try assign current value of time(x0) itself: > settime(x0) <- gettime(x0) error: initialize timeseries : length of '@positions' not equal '@.data' extent
is bug in "timeseries" package?
from documentation of "timeseries" package learn "gettime" synonym "time". hence looking source code of function:
> getmethod("time",signature=c(x="timeseries")) method definition: function (x, ...) .time.timeseries(x, ...) <environment: namespace:timeseries> signatures: x target "timeseries" defined "timeseries"
so need source code of function, ".time.timeseries", used internally "timeseries" package:
> getanywhere(".time.timeseries") single object matching ‘.time.timeseries’ found found in following places package:timeseries namespace:timeseries value function (x, ...) { if (length(x@positions) > 0) timedate(x@positions, zone = "gmt", fincenter = x@fincenter) else seq.int(nrow(x)) } <environment: namespace:timeseries> >
here see bug. "seq.int(nrow(x))" sequence 1,...,nrow(x)
in steps of 1, if nrow(x)>=1, but
in steps of -1, if nrow(x)<1.
hence in particular case, length of time series 0, value of "time" sequence "c(1,0)". 1 can fix follows:
seq.int(1,nrow(x),length.out=nrow(x))
gives rather correct value "integer(0)" if "nrow(x)" 0.
Comments
Post a Comment