title: How can I drop unused levels from a data frame?
body: <p>Given the following mock data:</p>
  
  <pre><code>set.seed(123)
  x &lt;- data.frame(let = sample(letters[1:5], 100, replace = T), 
                  num = sample(1:10, 100, replace = T))
  y &lt;- subset(x, let != 'a')
  </code></pre>
  
  <p>Creating a table of <code>y$let</code> yields</p>
  
  <pre><code>a  b  c  d  e 
  0 20 21 22 18
  </code></pre>
  
  <p>But I don't want <code>a</code> to show anymore. If I try to do this:</p>
  
  <pre><code>levels(y$let) &lt;- factor(y$let)
  </code></pre>
  
  <p>I mess the frequencies, since now <code>table(y$let)</code> gives me</p>
  
  <pre><code>b  d  c  e 
  0 20 21 40 
  </code></pre>
  
  <p>I'm aware I could do <code>xtabs(~ y$let, drop.unused.levels = T)</code> and work around the problem, but it doesn't reset the variable levels at its core (which is important to me, since this is an early change I'm making to the dataset which will carry on throughout the whole analysis). Moreover, <code>xtabs</code> is a different class from <code>table</code>, which will give me headaches later in the project.</p>
  
  <p>The question is: how can I automatically change <code>levels(y$let)</code> so it doesn't show levels that were dropped when I created the subset? In this case, how can I make it show <code>[1] "b" "c" "d" "e"</code>?</p>