[R] Summarize by two or more attributes

Marc Schwartz marc_schwartz at me.com
Tue May 17 21:41:38 CEST 2011


On May 17, 2011, at 12:53 PM, LCOG1 wrote:

> I will hit my own ball on this one
> 
> 
> tapply(Df$Rate,list(Df$Bin,Df$Type),sum)
> 


Aha....you had mentioned creating a new column in your initial post, presumably added to 'Df', as opposed to creating a new independent matrix of the results.

Your output above creates a 5 x 2 matrix of the resultant sums, one column per 'Type' and one row for each 'Bin'.

The use of ave(), now based upon your above:

  ave(Df$Rate, list(Df$Bin, Df$Type), FUN = sum)

would yield a vector of length 20, which could then be added to the original 'Df' as a new column. The vector would be ordered in such a fashion as to match up with the original rows, based upon Bin and Type.

I am tempted to quote a famous line from Cool Hand Luke, but I'll leave that for now...  :-)

Regards,

Marc Schwartz



More information about the R-help mailing list