Question

I have a list of zoo elements. And the list looks like this:

> str(a)
List of 4
  $ :‘zoo’ series from 2013-08-08 to 2013-09-09
  Data: num [1:33] 0 0 0 0 5.62 ...
  Index:  POSIXct[1:33], format: "2013-08-08" "2013-08-09" "2013-08-10" "2013-08-11" ...
 $ :‘zoo’ series from 2013-08-08 to 2013-09-09
  Data: num [1:33] 0 0 0 0 0 ...
  Index:  POSIXct[1:33], format: "2013-08-08" "2013-08-09" "2013-08-10" "2013-08-11" ...
 $ :‘zoo’ series from 2013-08-08 to 2013-09-09
  Data: num [1:33] 7.4 10.7 0 0 0 ...
  Index:  POSIXct[1:33], format: "2013-08-08" "2013-08-09" "2013-08-10" "2013-08-11" ...
 $ :‘zoo’ series from 2013-08-08 to 2013-09-09
  Data: num [1:33] 0 588 0 0 0 ...
  Index:  POSIXct[1:33], format: "2013-08-08" "2013-08-09" "2013-08-10" "2013-08-11" ...

Right now, I can plot the sum of those zoo elements by doing so:

plot(z <- a[[1]] + a[[2]] + a[[3]] + a[[4]])

I am wondering is there an easy way to do that to add up all the elements inside a list. Something like

# PYSEUDO CODE NOT WORKING!
plot(z <- foo(a))

Thanks a lot!

Was it helpful?

Solution

I'm not sure I fully understand, but I think you're looking for Reduce(), if not please provide an example with sample data and your desired results. Consider this example:

> a <- list(rnorm(5),rnorm(5),rnorm(5),rnorm(5))
> a[[1]] + a[[2]] + a[[3]] + a[[4]]
[1]  1.06310824 -0.20176594 -2.09269676  0.01593767 -1.30949132
> Reduce("+",a)
[1]  1.06310824 -0.20176594 -2.09269676  0.01593767 -1.30949132
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top