title: Average only duplicated rows and replacing value in a defined column
body: <p>I have a dataframe <code>D</code>:</p>
  
  <pre><code>surname   name   salary
  Red        A      1000
  Green      B       900
  Green      A      1100
  Blue       C      1000
  Blue       B      1000
  Blue       F       800
  Violet     F      1200
  </code></pre>
  
  <p>Some row has no replicate in <code>surname</code>, some other yes. </p>
  
  <p>I need to aggregate the rows only where surname is duplicated, to average values of salary and change the name to "X". </p>
  
  <p>I tryed something using <code>duplicated()</code> but it leave one duplicate as original and change the others.</p>
  
  <pre><code>D$name&lt;-replace(D$name,duplicated(D$surname),"X")
  </code></pre>
  
  <p>And also I was unable to average the values of <code>salary</code>.</p>
  
  <p>Thank you!</p>