title: Collapsing Factors within hierarchy
body: <p>Is there a pre-built way to collapse factors within a hierarchy?  I know I can collapse manually.  I know I can use the fct_lump functions to collapse my rarest factors automatically.  But if I need that to 'respect' a hierarchy is there a way?</p>
  <p>In its simplest version - if I have a factor with pets and have 5 levels: Cat (9), Dog (10), Rabbit (4), Guinea Pig (3) and Fish (2).  If I use fct_lump_n(3), I would get Guinea Pig and Fish lumped together.  In my data, I want to put Rabbit and Guinea Pig together as either 'Other Mammals' or 'Rhodents'.  I can obviously manually do that with fct_collapse.  But is there any way, by adding the rest of the hierarchy to my data structure that this could be automatic?</p>
  <p>This is a crude example of my data:</p>
  <pre><code>require(tidyverse)
  
  myData &lt;- tibble (
      pet = as_factor(c(&quot;dog&quot;, &quot;cat&quot;, &quot;guinea pig&quot;, &quot;rat&quot;, &quot;rabbit&quot;, &quot;hamster&quot;, &quot;mouse&quot;, &quot;gerbil&quot;, &quot;goldfish&quot;, &quot;tropical fish&quot;,
              &quot;snake&quot;, &quot;gecko&quot;, &quot;tortoise&quot;, &quot;chicken&quot;, &quot;budgie&quot;, &quot;parrot&quot;, 
              &quot;dog&quot;, &quot;cat&quot;, &quot;guinea pig&quot;,  &quot;rabbit&quot;, &quot;hamster&quot;, &quot;gerbil&quot;, &quot;goldfish&quot;, 
              &quot;snake&quot;, &quot;gecko&quot;, &quot;budgie&quot;, 
              &quot;dog&quot;, &quot;cat&quot;, &quot;guinea pig&quot;, &quot;dog&quot;, &quot;dog&quot;, &quot;dog&quot;, &quot;dog&quot;, &quot;cat&quot;, &quot;cat&quot;, &quot;cat&quot;,
              &quot;cat&quot;, &quot;dog&quot;, &quot;cat&quot;, &quot;dog&quot;, &quot;cat&quot;, &quot;dog&quot;, &quot;chicken&quot;, &quot;rabbit&quot;, &quot;rabbit&quot;, &quot;hamster&quot;)
  ))
  
  myClassifications &lt;- tibble (
      pet = as_factor(c(&quot;dog&quot;, &quot;cat&quot;, &quot;guinea pig&quot;, &quot;rat&quot;, &quot;rabbit&quot;, &quot;hamster&quot;, &quot;mouse&quot;, &quot;gerbil&quot;, &quot;goldfish&quot;, &quot;tropical fish&quot;,
              &quot;snake&quot;, &quot;gecko&quot;, &quot;tortoise&quot;, &quot;chicken&quot;, &quot;budgie&quot;, &quot;parrot&quot;)),
      class = as_factor(c(&quot;mammal&quot;, &quot;mammal&quot;, &quot;mammal&quot;, &quot;mammal&quot;, &quot;mammal&quot;, &quot;mammal&quot;, &quot;mammal&quot;, &quot;mammal&quot;,  &quot;fish&quot;, &quot;fish&quot;, &quot;reptile&quot;, &quot;reptile&quot;, &quot;reptile&quot;, &quot;bird&quot;, &quot;bird&quot;, &quot;bird&quot;)),
      family = as_factor(c(&quot;canine&quot;, &quot;feline&quot;, &quot;rodent&quot;,&quot;rodent&quot;,&quot;rodent&quot;,&quot;rodent&quot;,&quot;rodent&quot;,&quot;rodent&quot;, &quot;cold water fish&quot;, &quot;warm water fish&quot;, &quot;legless repitle&quot;, &quot;repitle with legs&quot;, &quot;reptile with legs&quot;, &quot;flightless bird&quot;, &quot;flighted bird&quot;, &quot;flighted bird&quot;))
  )
  
  # Add classifcations to my data
  
  myData %&gt;%
      left_join(myClassifications, by=&quot;pet&quot;) -&gt; myData
  
  </code></pre>
  <p>The basic output is like this:</p>
  <pre><code>&gt; myData %&gt;%
       group_by(pet) %&gt;% #class, family, pet) %&gt;%
       summarise(n())
  </code></pre>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">pet</th>
  <th style="text-align: right;">n()</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">dog</td>
  <td style="text-align: right;">10</td>
  </tr>
  <tr>
  <td style="text-align: left;">cat</td>
  <td style="text-align: right;">9</td>
  </tr>
  <tr>
  <td style="text-align: left;">guinea pig</td>
  <td style="text-align: right;">3</td>
  </tr>
  <tr>
  <td style="text-align: left;">rat</td>
  <td style="text-align: right;">1</td>
  </tr>
  <tr>
  <td style="text-align: left;">rabbit</td>
  <td style="text-align: right;">4</td>
  </tr>
  <tr>
  <td style="text-align: left;">hamster</td>
  <td style="text-align: right;">3</td>
  </tr>
  <tr>
  <td style="text-align: left;">mouse</td>
  <td style="text-align: right;">1</td>
  </tr>
  <tr>
  <td style="text-align: left;">gerbil</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: left;">goldfish</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: left;">tropical fish</td>
  <td style="text-align: right;">1</td>
  </tr>
  <tr>
  <td style="text-align: left;">snake</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: left;">gecko</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: left;">tortoise</td>
  <td style="text-align: right;">1</td>
  </tr>
  <tr>
  <td style="text-align: left;">chicken</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: left;">budgie</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: left;">parrot</td>
  <td style="text-align: right;">1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I'd want to try and get to something like:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">pet</th>
  <th style="text-align: right;">n()</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">dog</td>
  <td style="text-align: right;">10</td>
  </tr>
  <tr>
  <td style="text-align: left;">cat</td>
  <td style="text-align: right;">9</td>
  </tr>
  <tr>
  <td style="text-align: left;">Rodents</td>
  <td style="text-align: right;">16</td>
  </tr>
  <tr>
  <td style="text-align: left;">Fish</td>
  <td style="text-align: right;">3</td>
  </tr>
  <tr>
  <td style="text-align: left;">Reptiles</td>
  <td style="text-align: right;">5</td>
  </tr>
  <tr>
  <td style="text-align: left;">Birds</td>
  <td style="text-align: right;">5</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I can likely do something with a for loop to do it.  But if there is a package or function that can do it for me it may give a less subjective way to doing it.  In effect what I think I am doing is starting at Mammals, Reptiles etc and Expanding the level if there is 'enough' data to be meaningful...  How we define meaningful of course is subjective!  In my table above there are 48 data points, and I didn't expand if the data was ~ &lt;10% of the overall. (similar to fct_lump_prop(0.10) )</p>
  <p>Is there a simple solution?</p>