0

I have a dataframe as such:

     Response  Spent    Saved
1       Yes     100      25 
2       Yes     200      50 
3       No       20       2
4       No       13       3

I would like to sum up the amounts Spent and Saved, depending on the Response, ie:

     Response  Spent    Saved
1       Yes     300      75 
2       No      33       5  

Right now, I am using a hackneyed approach, where I subset the dataframe into 2 new dataframes, convert the 2nd and 3rd columns into numeric data, do a colSums on each column individually, then save the outputs into a vector, then create a new dataframe....suffice to say it is a terrible approach.

How could I do this is a more effective manner?

Thanks for reading

Pierre L
  • 28,203
  • 6
  • 47
  • 69
tumultous_rooster
  • 12,150
  • 32
  • 92
  • 149

1 Answers1

3

Check ?aggregate

If your data.frame is DF, following should do what you want.

aggregate(. ~ Response, data = DF, FUN = sum)
##   Response Spent Saved
## 1       No    33     5
## 2      Yes   300    75
CHP
  • 16,981
  • 4
  • 38
  • 57
  • Aha! So in English: "Aggregate everything, with respect to Response, where the dataframe is DF, and the aggregation function is summation." Perfect! – tumultous_rooster Apr 15 '14 at 02:13