[R] question about time series objects

David Winsemius
Sat, 30 Jan 2010 15:22:19 -0000

On Jan 30, 2010, at 9:22 AM, Dipankar Basu 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)?

Time series (at least as I understand them) are vectors or matrices
with a folded and labeled print method but they are not referenced by
the marginal indices that are offered in the print method. Assuming
you followed that set of examples and have a time series named, jj,
the specific element could be accessed as:

 > yr=1961
 > qtr=3
 > jj[ 4*(yr-1960)+qtr ]
[1] 0.92

Or if you wanted to extract the start year programmatically:

 > jj[4*(yr-attr(jj, "tsp")[1])+qtr]
[1] 0.92

 > jj[4*(yr-start(jj))+qtr]
[1] 0.92   NA

That NA comes from the return values of 1960 and 1 using start. The
help page for start has no section that describes the values, but it
appears that you get both the year and qtr.

 > jj[4*(yr-start(jj)[1])+qtr]
[1] 0.92   # what I expected

--

David Winsemius, MD
Heritage Laboratories
West Hartford, CT