title: Using cut() to make factor
body: <p>I am trying to make a factor variable out of a numeric variable in R. I would like to keep track of NA's and the new bins I am creating. Within the new bins, some numbers are of a valid range and some are not. I care about the bins themselves but want to create an "invalid" level that will house anything that does not fall in a designated range.</p>
  
  <p>Here is an example:</p>
  
  <pre><code>library(reshape)
  
  fac &lt;- c(-1, 1, 2, 3, 4, 100, NA)
  fac &lt;- cut(fac, c(-Inf, 1, 2, 3, Inf))
  fac &lt;- addNA(fac)
  combine_factor(fac, 
             variable=order(levels(fac))[c(2,3,5)],
             other.label = "Invalid")
  </code></pre>
  
  <p>Which would give me some output that would have the levels I want to be intervals, NA, or invalid.</p>
  
  <p>However, the trouble I am having is I do not want to code the variable using numbers because I have multiple different data sets and not all of them contain each level of the factor. </p>
  
  <p>If I change the variable so that it does not contain any of a certain level of the factor:</p>
  
  <pre><code>fac &lt;- c(-1, 1, 3, 4, 100, NA)
  </code></pre>
  
  <p>I keep getting the error:</p>
  
  <blockquote>
    <p>Error in factor(nvar[as.numeric(fac)], labels=c(levels(fac)[variable],
    : invalid 'labels'; length 4 should be 1 or 3.</p>
  </blockquote>
  
  <p>Output 1 (which works because I have no levels occurring 0 times):</p>
  
  <pre><code>[1] (1,2]   (1,2]   (2,3]   &lt;NA&gt;    Invalid Invalid Invalid
  Levels: (1,2] (2,3] &lt;NA&gt; Invalid
  </code></pre>
  
  <p>Output 2 (where one level: (1,2] has 0 occurrences): </p>
  
  <pre><code>[1]   (2,3]   &lt;NA&gt;    Invalid Invalid Invalid 
  Levels: (1,2] (2,3] &lt;NA&gt; Invalid
  </code></pre>
  
  <p>The second scenario is where I experience the error.</p>
  
  <p>Is there any way I can get around this error?</p>