title: Displaying of factor levels and labels in R
body: <p>I am having an issue with displaying the correct grouping of a factor variable after using MICE. I believe this is an R thing, but I included it with mice just to be sure.</p>
  
  <p>So, I run my mice algorithm, here is a snipit of how I call I format it in the mice algorithm. Note, I want it to be 0 for no drug, and 1 for yes drug, so I coerce it to be a factor with levels 0 and 1 before I run it</p>
  
  <pre><code>mydat$drug=factor(mydat$drug,levels=c(0,1),labels=c(0,1))
  </code></pre>
  
  <p>I then run mice and it runs logistic regression (this is the default) on drug, along with my other variables to be imputed.</p>
  
  <p>I can extract the results of one of the imputations when it is complete by</p>
  
  <pre><code>drug=complete(imp,1)$drug
  </code></pre>
  
  <p>We can view it</p>
  
  <pre><code>&gt; head(drug)
  [1] 0 0 1 0 1 1
  attr(,"contrasts")
    2
  0 0
  1 1
  Levels: 0 1
  </code></pre>
  
  <p>So the data is certainly 0,1.</p>
  
  <p>However, when I do something with it, like cbind, it changes to 1's and 2's</p>
  
  <pre><code>&gt; head(cbind(drug))
   drug
  [1,]    1
  [2,]    1
  [3,]    2
  [4,]    1
  [5,]    2
  [6,]    2
  </code></pre>
  
  <p>Even when I coerce it to a numeric</p>
  
  <pre><code>&gt; head(as.numeric(drug))
  [1] 1 1 2 1 2 2
  </code></pre>
  
  <p>I want to say it has something to do with the contrasts, but when I delete the contrast by doing </p>
  
  <pre><code>attr(drug,"contrasts")=NULL
  </code></pre>
  
  <p>It still shows up with 1's and 2's when called and printed by others.</p>
  
  <p>I am able to get it to print correctly by using I()</p>
  
  <pre><code>&gt; head(I(drug))
  [1] 0 0 1 0 1 1
  Levels: 0 1
  </code></pre>
  
  <p>So, I believe that this is an R issue, but I don't know how to remedy it. Is using I() the correct solution, or is it just a workaround that happens to work here? What is actually happening behind the scenes that is making the output display as 1's and 2's?</p>
  
  <p>Thanks</p>