Dear list members, what is the reason that one obviously can't do arithmetic operations on zoo members with different index positions? > require(zoo) > z <- zoo(c(1,1,1),order.by=c(1,2,3)) > z 1 2 3 1 1 1 > z[1] + z[1] 1 2 > z[1:2] + z[1:2] 1 2 2 2 > z[1] + z[2] Data: numeric(0) Index: numeric(0)
On Oct 23, 2011, at 9:35 PM, Hugo Mildenberger wrote:> Dear list members, > > what is the reason that one obviously can't do arithmetic operations > on > zoo members with different index positions?zoo-objects are designed to merged by their indices before applying arithmetic operations. You are essentially adding across rows of: > merge(z[1],z[2]) z[1] z[2] 1 1 NA 2 NA 1 You have no overlapping indices so you get nothing. > z2 <- zoo(10:15,order.by=1:6) > z2+z 1 2 3 11 12 13 # Only get results where the indices "line up".> >> require(zoo) >> z <- zoo(c(1,1,1),order.by=c(1,2,3)) >> z > 1 2 3 > 1 1 1 >> z[1] + z[1] > 1 > 2 >> z[1:2] + z[1:2] > 1 2 > 2 2 >> z[1] + z[2] > Data: > numeric(0) > > Index: > numeric(0) > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
On Mon, 24 Oct 2011, Hugo Mildenberger wrote:> Dear list members, > > what is the reason that one obviously can't do arithmetic operations on > zoo members with different index positions?It's a _feature_ that zoo first matches the index positions, uses only their intersection, and then performs arithmetic. This is also documented in Section 2.5 of vignette("zoo", package = "zoo"). The reason for this behaviour is, of course, that you would want to align computations between different series, in particular a series and its lags. If you want to ignore the time index of one or more elements in mathematical operations, use coredata(z). hth, Z> > > require(zoo) > > z <- zoo(c(1,1,1),order.by=c(1,2,3)) > > z > 1 2 3 > 1 1 1 > > z[1] + z[1] > 1 > 2 > > z[1:2] + z[1:2] > 1 2 > 2 2 > > z[1] + z[2] > Data: > numeric(0) > > Index: > numeric(0) > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >