title: unsplit list, merge factors
body: <p>I have the following data frame in R:</p>
  
  <pre><code>  c1 c2  
  1 10  a  
  2 20  a  
  3 30  b  
  4 40  b
  </code></pre>
  
  <p>I then <code>split</code> it as follows: <code>z = lapply(split(test$c1, test$c2), function(x) {cut(x,2)})
  </code>.  <code>z</code> is then: </p>
  
  <pre><code>$a  
  [1] (9.99,15] (15,20]  
  Levels: (9.99,15] (15,20]
  
  $b  
  [1] (30,35] (35,40]
  Levels: (30,35] (35,40]  
  </code></pre>
  
  <p>I would like to then merge the factors back by unsplitting the list <code>unsplit(z, test$c2)</code>. This generates a warning:</p>
  
  <pre><code>[1] (9.99,15] (15,20]   &lt;NA&gt;      &lt;NA&gt;     
  Levels: (9.99,15] (15,20]
  Warning message:
  In `[&lt;-.factor`(`*tmp*`, i, value = 1:2) :
    invalid factor level, NAs generated
  </code></pre>
  
  <p>I would like to take a union of all the factor levels and then unsplit so that this error does not happen:</p>
  
  <pre><code>z$a = factor(z$a, levels=c(levels(z$a), levels(z$b)))
  unsplit(z, test$c2)
  [1] (9.99,15] (15,20]   (30,35]   (35,40]  
  Levels: (9.99,15] (15,20] (30,35] (35,40]    
  </code></pre>
  
  <p>In my real data frame I have a very big list so I need to iterate over all the list elements (not just two).  What is the best way to do this?</p>