title: Using apply() function to update the factor levels of multiple columns of a data frame in R
body: <p>Straight to the question. Say I have a following data frame:</p>
  
  <pre><code>&gt; head(temp)
    Gender Age Agegroup
  2   Male  63      61+
  3   Male  60    50-60
  4   Male  55    50-60
  5   Male  36    30-39
  7   Male  39    30-39
  8   Male  63      61+
  </code></pre>
  
  <p>Calling a summary function:</p>
  
  <pre><code>&gt; summary(temp)
      Gender            Age         Agegroup     
   Male  :864692   Min.   :25.00   25-29:0  
   Female:     0   1st Qu.:35.00   30-39:205237  
                   Median :45.00   40-49:235622  
                   Mean   :44.48   50-60:250977  
                   3rd Qu.:54.00   61+  : 68807  
                   Max.   :64.00   
  </code></pre>
  
  <p>As you can see there are zero observations for the Female factor and 25-29 factor levels. As a result, I dont need those levels. I remove them using the following code:</p>
  
  <pre><code>temp$Gender&lt;-factor(temp$Gender)
  temp$Agegroup&lt;-factor(temp$Agegroup)
  </code></pre>
  
  <p>My question is: how would I use the one of the apply function to execute the code I used to remove levels? Something that will look like:</p>
  
  <pre><code># Pseudo code just to illustrate my purpose
  temp[,c(1,3)]&lt;-apply(temp[,c(1,3)],FUN=factor)
  </code></pre>
  
  <p>It will be handy in case I need to update the levels of lots of columns of a data frame. Thanks. Let me know if you need more clarification. </p>