[R] Subsetting Time Series

Richard A. Bilonick rab at nauticom.net
Thu Apr 17 18:23:56 CEST 2003


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 :-) 
>}
>
>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			<><
>
>
>  
>
There does seem to be a problem in 1.6.2 - or am I missing something? 
First, x is a ts object. Once I use subsetting notation to replace one 
value, x does not seem to print correctly, and is.ts reports FALSE:

 > x <- ts(1:12,start=c(2003,1),frequency=12)
 > x
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2003   1   2   3   4   5   6   7   8   9  10  11  12
 > x[1]
[1] 1
 > x[1] <- 9
 > x
 [1]  9  2  3  4  5  6  7  8  9 10 11 12
attr(,"tsp")
[1] 2003.000 2003.917   12.000
attr(,"class")
[1] "ts"
 > x
 [1]  9  2  3  4  5  6  7  8  9 10 11 12
attr(,"tsp")
[1] 2003.000 2003.917   12.000
attr(,"class")
[1] "ts"
 > as.ts(x)
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2003   9   2   3   4   5   6   7   8   9  10  11  12
 > is.ts(x)
[1] FALSE

Is it supposed to work this way?

Rick B.



More information about the R-help mailing list