[R] understanding time series objects

Achim Zeileis zeileis at ci.tuwien.ac.at
Fri Aug 8 14:07:45 CEST 2003


On Friday 08 August 2003 12:46, Angel wrote:

> ### First, is there a way to access a univariate time series as a
> matrix instead of a vector?
> # For example:
> data(UKLungDeaths)
> # If I do
> apply(mdeaths,1,cumsum)
> # Gives an error as mdeaths is not a matrix but a vector,

Yes, a single time series in R is essentially a vector plus the time 
series properties. Therefore, it can be accessed like a vector.

> although
> when I look at it :
> mdeaths
> # the ts object has a matrix like "appearance"

That is a print option that can be turned off, look at
  help(print.ts)

> # The only way of doing it I've found is:
> mdeaths2<-as.matrix(mdeaths)
> dim(mdeaths2)<-c(12,6)
> mdeaths2<-apply(mdeaths2,2,cumsum)
> mdeaths[]<-mdeaths2
> # It is not very efficient to solve the problem of applying a
> cummulative sum to each year
>
> ### Second, for a multivariate ts:
> data(UKLungDeaths)
> Multits<-ts.union(mdeaths, fdeaths)
> # Why does
> Multits$mdeaths
> # not work and I have to use:
> Multits[,"mdeaths"]
> # Is it the way it works or am I missing something?

A multivariate time series ("mts" object) is essentially a matrix plus 
time series properties. Therefore, it can be accessed like a matrix 
(and not like a data.frame/list).

hth,
Z


> # Thanks as always,
> # Angel
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list