title: Tidyverse: filtering n largest groups in grouped dataframe
body: <p>I want to filter the n largest groups based on count, and then do some calculations on the filtered dataframe</p>
  
  <p>Here is some data </p>
  
  <pre><code>Brand &lt;- c("A","B","C","A","A","B","A","A","B","C")
  Category &lt;- c(1,2,1,1,2,1,2,1,2,1)
  Clicks &lt;- c(10,11,12,13,14,15,14,13,12,11)
  df &lt;- data.frame(Brand,Category,Clicks)
  
  |Brand | Category| Clicks|
  |:-----|--------:|------:|
  |A     |        1|     10|
  |B     |        2|     11|
  |C     |        1|     12|
  |A     |        1|     13|
  |A     |        2|     14|
  |B     |        1|     15|
  |A     |        2|     14|
  |A     |        1|     13|
  |B     |        2|     12|
  |C     |        1|     11|
  </code></pre>
  
  <p>This is my expected output. I want to filter out the two largest brands by count and then find the mean clicks in each brand / category combination</p>
  
  <pre><code>|Brand | Category| mean_clicks|
  |:-----|--------:|-----------:|
  |A     |        1|        12.0|
  |A     |        2|        14.0|
  |B     |        1|        15.0|
  |B     |        2|        11.5|
  </code></pre>
  
  <p>Which I thought could be achieved with code like this (but can't)</p>
  
  <pre><code>df %&gt;%
    group_by(Brand, Category) %&gt;%
    top_n(2, Brand) %&gt;% # Largest 2 brands by count
    summarise(mean_clicks = mean(Clicks))
  </code></pre>
  
  <p>EDIT: the ideal answer should be able to be used on database tables as well as local tables</p>