[R] Summing rows by years (each time separately)

William Dunlap wdunlap at tibco.com
Mon Jan 9 17:13:59 CET 2012


You didn't show us what you did to get the results
you didn't want.  I suspect you did not compute
   groupId <- cumsum(c(TRUE, year[-1] != year[-length(year)]))
and aggregate by groupId instead of by year.  (Note
that groupId counts how many changes there are
in the sequence of years.)

By the way, package plyr's functions do what aggregate()
does and more and you may prefer them.

  > d <- data.frame(
          year = c(2008, 2008, 2008, 2009, 2009, 2008, 2008),
          sales = c(  1,    2,    4,    8,   16,   32,   64),
          costs = c(  1,    3,    27,  81,  243,  729,  2187))
  > d$groupId <- with(d, cumsum(c(TRUE, year[-1] != year[-length(year)])))
  > d
    year sales costs groupId
  1 2008     1     1       1
  2 2008     2     3       1
  3 2008     4    27       1
  4 2009     8    81       2
  5 2009    16   243       2
  6 2008    32   729       3
  7 2008    64  2187       3
  > ddply(d, .(groupId), function(di)with(di, c(year=year[1],
               sales=sum(sales), costs=sum(costs), n=length(year))))
    groupId year sales costs n
  1       1 2008     7    31 3
  2       2 2009    24   324 2
  3       3 2008    96  2916 2

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Andrew Gaska
> Sent: Monday, January 09, 2012 6:07 AM
> To: r-help at r-project.org
> Subject: Re: [R] Summing rows by years (each time separately)
> 
> Thanks for your answer!
> 
> There is one thing I could not find in aggregate():
> 
>  I want to have it a sum for each group of 2008,2009 and 2010. In
> aggregate() I can sum all the rows that have a rowname 2008,  all the rows
> that have a rowname 2009 and all the rows that have a rowname 2010.
> 
> But I want to have it summed separately each time a new group starts, so not
> like this:
> 
> 2008 24 20 16
> 2009 19 12 17
> 2010 16 19 13
> 
> but like this:
> 
> 2008        15	12	10
> 2009        9	3	7
> 2010        10	14	10
> 2008        9	8	6
> 2009        10	9	10
> 2010        6	5	3
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Summing-rows-by-years-each-time-
> separately-tp4276428p4278550.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list