Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts.intersect does not work with xts objects #229

Open
joshuaulrich opened this issue Mar 17, 2018 · 0 comments
Open

ts.intersect does not work with xts objects #229

joshuaulrich opened this issue Mar 17, 2018 · 0 comments
Labels

Comments

@joshuaulrich
Copy link
Owner

Taken from a Stack Overflow question:

The following produces an error

a1 = as.xts(ts(rnorm(20), start=c(1980,1), freq=4))
a2 = as.xts(ts(rnorm(30), start=c(1983,1), freq=4))
a = ts.intersect(a1,a2)

Error in .cbind.ts(list(...), .makeNamesTs(...), dframe = dframe, union = FALSE) : 
  no time series supplied

The documentation says argument should be, "two or more univariate or multivariate time series, or objects which can coerced to time series."

My response:

ts.intersect determines whether the objects is a ts object by looking for the tsp attribute. as.xts.ts removes the tsp attribute, which is why it is not coerced back to a ts object.

This looks like a bug in xts->ts->xts conversion, but I need to take a closer look.

As a work-around, you can manually add the tsp attribute to your xts object (note that this might cause issues with other xts methods, e.g. str.xts) and add a .tsp attribute as well.

set.seed(21)
A1 <- ts(rnorm(20), start=c(1980,1), freq=4)
A2 <- ts(rnorm(30), start=c(1983,1), freq=4)
# convert to xts
a1 <- as.xts(A1)
a2 <- as.xts(A2)
# add tsp attribute
# (so stats:::.cbind.ts will think these are coercible to ts objects)
tsp(a1) <- tsp(A1)
tsp(a2) <- tsp(A2)
# add .tsp attribute
# (needed for as.ts.xts to work)
attr(a1,'.tsp') <- tsp(A1)
attr(a2,'.tsp') <- tsp(A2)

a <- ts.intersect(a1,a2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant