0

What's an elegant way to calculate the ratio of two columns (NAs included) across all groups? Let's say we have this data:

 Name Col1 Col2
  Abe    2    3
  Abe    1    2
 Brad    2    1
 Brad    2    2
Chuck    1    1
Chuck    3    3

To calculate the ratio of Col:Col2 for each Name, we first add the values for each name per column before obtaining the ratio.

In this case, our answer is:

 Name Col1/Col2
  Abe       3/5
 Brad       4/3 
Chuck       4/4
  • 3
    Pick your favorite method [to sum by group (R-FAQ link)](http://stackoverflow.com/q/1660124/903061), then divide. If you have trouble with that, please show your attempt so we can help you correct it. Otherwise I would recommend closing as duplicate. – Gregor Thomas Feb 06 '17 at 18:31
  • Is there a way to close it without it negatively impacting me? I think I got the answer `aggregate(.~Name,df,Col1/Col2)` –  Feb 06 '17 at 18:36
  • 1
    A duplicate doesn't negatively impact you at all. It just sticks around as a pointer to help people find all the answers at the FAQ. – Gregor Thomas Feb 06 '17 at 18:37
  • 3
    I wouldn't worry. We have a guy that answers all the duplicates. – Rich Scriven Feb 06 '17 at 18:38
  • 1
    @RichScriven Oh hey Rich! Long time no see! I remember you helping me out a few... 10+ months ago! –  Feb 06 '17 at 18:49

0 Answers0