title: replace some row data by means in R
body: <p>I have a dataset from lab analyses, but it looks like there was an error in the lab for one sample as the data is quite different to all other data. I would like to take the mean of other replicates from the same sample to replace the &quot;bad data.&quot;</p>
  <p>This is what the data looks like now (notice the last row):</p>
  <pre><code>&gt; Season    Location    Var1    Var2   Var3
  &gt; spring    here         12      14     12
  &gt; spring    here         13      13     16
  &gt; spring    here         11      15     12
  &gt; spring    there        13      11     14
  &gt; spring    there        12      12     13
  &gt; spring    there        15      14     15
  &gt; summer    here         12      14     12
  &gt; summer    here         13      13     16
  &gt; summer    here         11      15     12
  &gt; summer    there        13      11     14
  &gt; summer    there        12      12     13
  &gt; summer    there         5       4      5
  </code></pre>
  <p>And this is how I would like it to look (notice that the last row is the mean of the preceding two rows, which are replicates of the same sample):</p>
  <pre><code>&gt; Season    Location    Var1    Var2   Var3
  &gt; spring    here         12      14     12
  &gt; spring    here         13      13     16
  &gt; spring    here         11      15     12
  &gt; spring    there        13      11     14
  &gt; spring    there        12      12     13
  &gt; spring    there        15      14     15
  &gt; summer    here         12      14     12
  &gt; summer    here         13      13     16
  &gt; summer    here         11      15     12
  &gt; summer    there        13      11     14
  &gt; summer    there        12      12     13
  &gt; summer    there        12.5    11.5   13.5
  </code></pre>
  <p>I have tried a few things including <code>Reduce()</code>, <code>aggregate()</code>, and <code>merge()</code>, but I'm finding it difficult to remove the data from the final row and replace it with the mean from the previous two rows while keeping the <code>Season</code> and <code>Location</code> variables.</p>