[R] An entire data frame which is a time-series?

Gabor Grothendieck ggrothendieck at myway.com
Tue Aug 17 18:27:12 CEST 2004


Ajay Shah <ajayshah <at> mayin.org> writes:

: 
: I have :
: 
: raw <- read.table("monthly.text", skip=3, sep="|",
:                 col.names=c("junk", "junk2",
:                   "wpi", "g.wpi", "wpi.primary", "g.wpi.primary",
:                   "wpi.fuel", "g.wpi.fuel", "wpi.manuf", "g.wpi.manuf",
:                   "cpi.iw", "g.cpi.iw", "cpi.unme", "g.cpi.unme",
:                   "cpi.al", "g.cpi.al", "cpi.rl", "g.cpi.rl"))
: 
: Now I can do things like:
: 
:   g.wpi = ts(raw$g.wpi, frequency=12, start=c(1994,7))
: 
: and it works fine. One by one, I can make time-series objects.
: 
: Is there a way to tell R that the entire data frame is a set of
: time-series, so that I don't have to go column by column and make a
: new ts() out of each?
: 
: I tried:
: 
:   M = ts(raw, frequency=12, start=c(1994,7))
:   ts.plot(M[,"wpi"], M[,"wpi.manuf"])
: 
: but this gives nonsense results. 

Converting a data frame to a ts object seems to work for me:

R> my.df <- data.frame(a = 1:4, b = 5:8)
R> my.ts <- ts(my.df, start=c(2000,4), freq=12)
R> my.ts.a <- my.ts[,"a"]
R> my.ts.a
     Apr May Jun Jul
2000   1   2   3   4

Suggest you provide a small reproduceable example that illustrates
the problem.


: Also, syntax like M$wpi is a lot
: nicer than M[,"wpi"]. Any ideas about what might work?

R> "$.ts" <- function(x, i) x[,i]
R> my.ts$a
     Apr May Jun Jul
2000   1   2   3   4




More information about the R-help mailing list