title: In R, how to efficiently de-dupe a data.frame while processing the duplicates?
body: <p>(The title of the question is terrible, I'm sorry. I was having a hard time finding a pithy way to express it.)</p>
  
  <p>I have a "tall" data.frame that I have compiled. It looks like this:</p>
  
  <pre><code>id | rating
  -----------
  3  | 5.5
  4  | 6
  4  | 7
  5  | 3
  5  | 5
  6  | 7.5
  7  | 9
  ...
  </code></pre>
  
  <p>I want to turn that into this:</p>
  
  <pre><code>id | avg rating
  -----------
  3  | 5.5
  4  | 6.5
  5  | 4
  6  | 7.5
  7  | 9
  ...
  </code></pre>
  
  <p>I don't just want to remove duplicates. I want to take the rows that have the same duplicate id, remove the duplicates, but update the rating field to be the average.</p>
  
  <p>I'm not sure how to go about this. I'm not even sure whether I should be modifying the original data frame or instead creating a new one with the modified data.</p>
  
  <p>(Note: I think a good answer would be a bit agnostic to the specifics of the operation. Like, if I wanted to do something similar but instead have the resulting rating column be a sum or a count, hopefully your answer would apply to those situations as well.)</p>