Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of rows based on column value

Tags:

r

plyr

I want to sum rows that have the same value in one column:

> df <- data.frame("1"=c("a","b","a","c","c"), "2"=c(1,5,3,6,2), "3"=c(3,3,4,5,2)) > df   X1 X2 X3 1  a  1  3 2  b  5  3 3  a  3  4 4  c  6  5 5  c  2  2 

For one column (X2), the data can be aggregated to get the sums of all rows that have the same X1 value:

> ddply(df, .(X1), summarise, X2=sum(X2))   X1 X2 1  a  4 2  b  5 3  c  8 

How do I do the same for X3 and an arbitrary number of other columns except X1?

This is the result I want:

  X1 X2 X3 1  a  4  7 2  b  5  3 3  c  8  7 
like image 745
R-obert Avatar asked Feb 24 '13 01:02

R-obert


People also ask

How do I sum rows in Excel based on cell value?

For example, the formula =SUMIF(B2:B5, "John", C2:C5) sums only the values in the range C2:C5, where the corresponding cells in the range B2:B5 equal "John." To sum cells based on multiple criteria, see SUMIFS function.

How do I sum specific rows and columns in Excel?

If you need to sum a column or row of numbers, let Excel do the math for you. Select a cell next to the numbers you want to sum, click AutoSum on the Home tab, press Enter, and you're done. When you click AutoSum, Excel automatically enters a formula (that uses the SUM function) to sum the numbers.


1 Answers

ddply(df, "X1", numcolwise(sum)) 

see ?numcolwise for details and examples.

like image 101
Ista Avatar answered Oct 18 '22 02:10

Ista