title: How to mutate sum by factor?
body: <p>I have a dataset that I am working on and need to sum up the amount by year. I would like to create a separate variable that sums up the amount just from one factor of another variable (example is just the United States amounts). Below is what I have to do these separately, how could I combine this code together?</p>
  
  <pre><code>country year donor amount
  china 2000 germany 20
  china 2000 france 30
  china 2000 united states 40 
  china 2000 united states 50
  china 2001 germany 20
  china 2001 france 30
  china 2001 united states 40 
  china 2001 united states 50
  china 2002 germany 20
  china 2002 france 30
  china 2002 united states 40 
  china 2002 united states 50
  
  new.data &lt;- old.data %&gt;%
    group_by(country, year) %&gt;%
    summarise(sum.amount = sum(amount)) %&gt;%
  
  new.data &lt;- old.data %&gt;%
    filter(donor == "United States")
    group_by(country, year) %&gt;%
    summarise(us.amount = sum(amount)) %&gt;%
  </code></pre>