2
votes

This should be exceptionally simple. I have a zoo object with 500 times series (each one a different product) and 250 periods of sales. The zoo object is perfectly rectangular, all series contain an observation at each point in time. My index column is a very simple 1...250,

My difficulty is in trying to aggregate all of the time series to form a "Total Sales" series.

I've tried using aggregate, which seems focused on aggregating rows e.g. days into months. But I want to keep every time period, just aggregate the time series together. This is a simplified version of my zoo object shown below with only 5 series.

head(z.all)

     1        2         3        4        5
 1 1232.205 1558.056  993.9784 1527.066 359.6946

 2 1262.194 1665.084 1092.0105 1834.313 484.5073

 3 1301.034 1528.607  900.4158 1587.548 525.5191

 4 1014.082 1352.090 1085.6376 1785.034 490.9164

 5 1452.149 1623.015 1197.3709 1944.189 600.5150

 6 1463.359 1205.948 1155.0340 1528.887 556.6371

When I try to aggregate using either of the following 2 commands I get exactly the same data as in my original zoo object!!

 aggregate(z.all[,1:num.series], index(z.all), sum)

 aggregate(z.all, index(z.all), sum)

However I am able to aggregate by doing this, though it's not realistic for 500 columns! I want to avoid using a loop if possible.

 z.all[,1] + z.all[,2]

Apologies if this is not the right protocol, it's my first post in this site.

1

1 Answers

4
votes

I hope i understood correctly what you want. But if it is a rowsum you are looking for:

 rowSums(z.all)

directly from the base package. (?rowSums). This function adds all values along one row:

D<-cbind(rep(1,10),c(1:10))
 colSums(D)
  [1] 10 55
 rowSums(D)
  [1]  2  3  4  5  6  7  8  9 10 11 

the opposite would be colSums() which would sum each column.