title: Cleaning up factor levels (collapsing multiple levels/labels)
body: <p>What is the most effective (ie efficient / appropriate) way to clean up a factor containing multiple levels that need to be collapsed? That is, how to combine two or more factor levels into one.</p>
  
  <p>Here's an example where the two levels "Yes" and "Y" should be collapsed to "Yes", and "No" and "N" collapsed to "No": </p>
  
  <pre><code>## Given: 
  x &lt;- c("Y", "Y", "Yes", "N", "No", "H")   # The 'H' should be treated as NA
  
  ## expectedOutput
  [1] Yes  Yes  Yes  No   No   &lt;NA&gt;
  Levels: Yes No  # &lt;~~ NOTICE ONLY **TWO** LEVELS
  </code></pre>
  
  <hr>
  
  <p>One option is of course to clean the strings before hand using <code>sub</code> and friends. </p>
  
  <p>Another method, is to allow duplicate label, then drop them</p>
  
  <pre><code>## Duplicate levels ==&gt; "Warning: deprecated"
  x.f &lt;- factor(x, levels=c("Y", "Yes", "No", "N"), labels=c("Yes", "Yes", "No", "No"))
  
  ## the above line can be wrapped in either of the next two lines
  factor(x.f)      
  droplevels(x.f) 
  </code></pre>
  
  <p>However, <strong>is there a more effective way</strong>? </p>
  
  <hr>
  
  <p>While I know that the <code>levels</code> and <code>labels</code> arguments should be vectors, I experimented with lists and named lists and named vectors to see what happens
  Needless to say, none of the following got me any closer to my goal. </p>
  
  <pre><code>  factor(x, levels=list(c("Yes", "Y"), c("No", "N")), labels=c("Yes", "No"))
    factor(x, levels=c("Yes", "No"), labels=list(c("Yes", "Y"), c("No", "N")))
  
    factor(x, levels=c("Y", "Yes", "No", "N"), labels=c(Y="Yes", Yes="Yes", No="No", N="No"))
    factor(x, levels=c("Y", "Yes", "No", "N"), labels=c(Yes="Y", Yes="Yes", No="No", No="N"))
    factor(x, levels=c("Yes", "No"), labels=c(Y="Yes", Yes="Yes", No="No", N="No"))
  </code></pre>