title: Select "middle" row from data frame with even total row number
body: <p>I have a data frame with two columns a and b, a defining the group and b being duplicate values for each group. I have added row numbers for illustrative purposes.</p>
  
  <pre><code>&gt; row.number &lt;- c(1:7)
  &gt; a &lt;- c(rep("A", 3), rep("B", 4))
  &gt; b &lt;- c(rep(250,3), rep(80,4))
  &gt; df &lt;-data.frame(row.number,a,b)
  &gt; df
    row.number a   b
  1          1 A 250
  2          2 A 250
  3          3 A 250
  4          4 B  80
  5          5 B  80
  6          6 B  80
  7          7 B  80
  </code></pre>
  
  <p>Now, I would like to remove the duplicate rows, retaining only the "middle" row. This is of course simple for groups with uneven total row numbers, but less straightforward with even total row numbers, where there is obviously no "middle" row. Here, I would like to retain the upper row, which is adjacent to the "middle"  The result should look like this:</p>
  
  <pre><code>&gt; row.number &lt;- c(2,5)
  &gt; a &lt;- c("A","B")
  &gt; b &lt;- c(250,80)
  &gt; df_solution &lt;- data.frame(row.number,a,b)
  &gt; df_solution
    row.number a   b
  1          2 A 250
  2          5 B  80
  </code></pre>
  
  <p>I have tried to write an ifelse function, where i) even/uneven total row numbers are evaluated (<code>nrow %% 2 == 0</code>), ii) if even then the row above the "middle" is retained, iii) if uneven the median row is retained. </p>
  
  <p>Using this <a href="https://stackoverflow.com/questions/31209715/collapse-duplicate-rows-by-median-value-in-r">post</a> as a starting point, I have tried various solutions, but I am not able to code anything sensible for step ii), i.e. defining the upper adjacent row of the middle for even total row numbers.</p>
  
  <p>Help would be much appreciated!</p>