[R] question about time series objects

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jan 30 17:07:47 CET 2010


See ?window.ts, e.g.

> # 1
> tt <- ts(c(0.71, 0.63, 0.85, 0.44, 0.61, 0.69, 0.92, 0.55),
+   start = 1960, frequency = 4)
> at <- c(1961, 3)
> window(tt, at, at)[1]
[1] 0.92

Note that 1961.00, 1961.25, 1961.50 and 1961.75 represent the 4
quarters of 1961 so this also works:

# 2
> window(tt, 1961.5, 1961.5)[1]
[1] 0.92

(Without the [1], the above commands return a time series of length 1
rather than just a number.)

A third approach is to convert to zoo.   as.zoo.ts converts a ts
series to a zoo series whose index can be accessed via year + fraction
notation and window.zoo supports an index argument (so we can avoid
having to specify the index twice):

> # 3
> library(zoo)
> coredata(window(as.zoo(tt), index = 1961.5))
[1] 0.92

(Without coredata it returns a length 1 zoo series rather than just a number.)

On Sat, Jan 30, 2010 at 9:22 AM, Dipankar Basu <basu.15 at gmail.com> wrote:
> Hi All,
>
> I have a very simple question about a time series object: how to access
> values for a particular year and quarter (say)?
>
> Suppose, following
>
> http://www.stat.pitt.edu/stoffer/tsa2/R_time_series_quick_fix.htm
>
> I have read in data as a time series; here is how it looks.
>
> *       Qtr1  Qtr2  Qtr3  Qtr4
>  1960  0.71  0.63  0.85  0.44
>  1961  0.61  0.69  0.92  0.55
>    .     .     .     .     .
>    .     .     .     .     .
>  1979 14.04 12.96 14.85  9.99
>  1980 16.20 14.67 16.02 11.61*
>
> How do I access the value for 1961 quarter 3 (say)?
>
> Dipankar
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list