[R] ts.intersect a multivariate and univariate ts

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jun 5 19:57:13 CEST 2005


On 6/5/05, Andy Bunn <abunn at whrc.org> wrote:
> > some_df[1, ] is actually a data frame: see ?"[.data.frame".  It's hard to
> > see what else it could be, as columns of a data frame are of arbitrary
> > classes.
> 
> I see, I was confusing class and mode. However, since a list can be a ts
> object as in this example:
> 
> R > w <- list(rnorm(10), rnorm(10))
> R > x <- ts(w, start = 1980)

Even though you don't get an error message this statement is 
erroneous.  ?ts discusses the valid possibilities.

> R > y <- ts(rnorm(10), start = 1981)
> R > tsp(x); tsp(y)
> [1] 1980 1981    1
> [1] 1981 1990    1
> R > class(x); class(y)
> [1] "ts"
> [1] "ts"
> R > mode(x); mode(y)
> [1] "list"
> [1] "numeric"
> R > z <- ts.intersect(x,y)
> Error: incorrect number of subscripts on matrix
> R >
> 
> What would be the easiest way to make x a mts that could be used with plot
> or ts.intersect?

The correct way to do this is to create a valid ts object from the
start such as any of the following:

  ts(do.call(cbind, w), start = 1980)
  ts(cbind(w[[1]], w[[2]]), start = 1980)
  ts(as.data.frame(w), start = 1980)
  ts(data.frame(w[[1]], w[[2]]), start = 1980)

(The resulting column names may differ depending on
which you use.)

If you already have an invalid object of the sort described by x in
your post then the following would do:

ts(do.call(cbind, x), start = start(x), frequency = frequency(x))

although in your case frequency(x) is the default, 1, so it could
be dropped.




More information about the R-help mailing list