title: Dplyr filter top and bottom rows by value simultaneously on grouped data
body: <pre><code># Sample dataframe
  set.seed(123)
  d = data.frame(x = runif(120), grp = gl(3, 40))
  
  # Select top_n
  d %&gt;%
    group_by(grp) %&gt;%
    top_n(n=3, wt=x)
  </code></pre>
  
  <p>How do I select both top and bottom observations within the same pipe?
  Have tried the following but does not work</p>
  
  <pre><code># helper function
  my_top_bott = function(x, n, wt) {
        x1 = x %&gt;% top_n(n=n, wt=wt)
        x2 = x %&gt;% top_n(n=n, wt=-wt)
        x = bind_rows(x1, x2)
        return(x)
      }
  
  # Pipe
  d %&gt;%
        group_by(grp) %&gt;%
        my_top_bott(., n=3, wt=x)
  </code></pre>