title: Elegant way to drop rare factor levels from data frame
body: <p>I want to subset a dataframe by factor. I only want to retain factor levels above a certain frequency. </p>
  
  <pre><code>df &lt;- data.frame(factor = c(rep("a",5),rep("b",5),rep("c",2)), variable = rnorm(12))
  </code></pre>
  
  <p>This code creates data frame:</p>
  
  <pre><code>   factor    variable
  1       a -1.55902013
  2       a  0.22355431
  3       a -1.52195456
  4       a -0.32842689
  5       a  0.85650212
  6       b  0.00962240
  7       b -0.06621508
  8       b -1.41347823
  9       b  0.08969098
  10      b  1.31565582
  11      c -1.26141417
  12      c -0.33364069
  </code></pre>
  
  <p>And I want to drop factor levels which repeated less than 5 times. I developed a for-loop and it is working:</p>
  
  <pre><code>for (i in 1:length(levels(df$factor))){
    if(table(df$factor)[i] &lt; 5){
      df.new &lt;- df[df$factor != names(table(df$factor))[i],] 
    }
  }
  </code></pre>
  
  <p>But do quicker and prettier solutions exists?</p>