[R] Subsetting Time Series
Martin Maechler
maechler at stat.math.ethz.ch
Thu Apr 17 18:20:48 CEST 2003
>>>>> "Richard" == Richard A Bilonick <rab at nauticom.net>
>>>>> on Thu, 17 Apr 2003 12:23:56 -0400 writes:
Richard> Martin Maechler wrote:
>> It seems you want to *replace* rather than *subset*
>> (otherwise, try to be more specific), and
>> there's no problem with that, e.g., with first two lines from
>> example(ts) :
>>
>>
>>
>> > gnp <- ts(cumsum(1 + round(rnorm(100), 2)), start = c(1954,7), frequency= 12)
>> > plot(gnp)
>> > gnp[20] <- 80
>> > str(gnp)
>> Time-Series [1:100] from 1954 to 1963: 2.47 3.26 4.50 5.18 6.31 ...
>> > lines(gnp, col=2)
>>
>> ------------
>>
>> If you really want to subset, you can use window(.) on your
>> time series, but only for those kinds of subsetting.
>> General subsetting doesn't give regularly spaced series.
>>
>> {"thinning" (e.g. taking every 2nd value) would be one kind of
>> subsetting that could be made to work ...
--> proposals please to R-devel at lists.R-project.org :-)
>> }
Richard> There does seem to be a problem in 1.6.2 - or am I
Richard> missing something? First, x is a ts object. Once I
Richard> use subsetting notation to replace one value, x
Richard> does not seem to print correctly, and is.ts reports
Richard> FALSE:
>> x <- ts(1:12,start=c(2003,1),frequency=12)
>> x
Richard> Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Richard> 2003 1 2 3 4 5 6 7 8 9 10 11 12
>> x[1]
Richard> [1] 1
>> x[1] <- 9
>> x
Richard> [1] 9 2 3 4 5 6 7 8 9 10 11 12
Richard> attr(,"tsp")
Richard> [1] 2003.000 2003.917 12.000
Richard> attr(,"class")
Richard> [1] "ts"
>> x
Richard> [1] 9 2 3 4 5 6 7 8 9 10 11 12
Richard> attr(,"tsp")
Richard> [1] 2003.000 2003.917 12.000
Richard> attr(,"class")
Richard> [1] "ts"
>> as.ts(x)
Richard> Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Richard> 2003 9 2 3 4 5 6 7 8 9 10 11 12
>> is.ts(x)
Richard> [1] FALSE
Richard> Is it supposed to work this way?
No. Yes, there's a bug in there,
but only when the time series is constructed from an *integer*
vector -- which is very rare in practice but happens in these
toy examples (and we had one report of practical data !)
Anyway,
---> your personal first reason to upgrade to version 1.7.0 !
If you use as.numeric(1:12) instead of 1:12
in your examples, all is well, even for R versions prior to 1.7
Martin Maechler <maechler at stat.math.ethz.ch> http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum LEO C16 Leonhardstr. 27
ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND
phone: x-41-1-632-3408 fax: ...-1228 <><
More information about the R-help
mailing list