Skip to content

question about time series objects

4 messages · Deepankar Basu, David Winsemius, Gabor Grothendieck

#
On Jan 30, 2010, at 9:22 AM, Dipankar Basu wrote:

            
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
#
See ?window.ts, e.g.
+   start = 1960, frequency = 4)
[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
[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):
[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: