title: Why does R change the variable type when prepending NA values to a data frame with factors?
body: <p>I have a problem with the way R coerces variable types when using <code>rbind</code> of two <code>data.frames</code> with <code>NA</code> values. I illustrate by example:</p>
  
  <pre><code>x&lt;-factor(sample(1:3,10,T))
  y&lt;-rnorm(10)
  dat&lt;-data.frame(x,y)
  NAs&lt;-data.frame(matrix(NA,ncol=ncol(dat),nrow=nrow(dat)))
  colnames(NAs)&lt;-colnames(dat)
  </code></pre>
  
  <p>Now the goal is to append <code>dat</code> and <code>NAs</code> while keeping the variable types <code>factor</code> and <code>numeric</code> of <code>x</code> and <code>y</code>. When I give:</p>
  
  <pre><code>dat_forward&lt;-rbind(dat,NAs)
  is.factor(dat_forward$x)
  </code></pre>
  
  <p>this works fine. However the backward direction using <code>rbind</code> fails:</p>
  
  <pre><code>dat_backward&lt;-rbind(NAs,dat)
  is.factor(dat_backward$x)
  is.character(dat_backward$x)
  </code></pre>
  
  <p>Now <code>x</code> is coerced to character level. I am confused - can't it stay factor type even if I use the other order of binding? What would be a straight forward change to my code to reach my goal?</p>