title: How to create dummy variables?
body: <p>I have a variable that is a factor :</p>
  
  <pre><code> $ year           : Factor w/ 8 levels "2003","2004",..: 4 6 4 2 4 1 3 3 7 2 ...
  </code></pre>
  
  <p>I would like to create 8 dummy variables, named "2003", "2004" etc that take the value 0 or 1 depending on the value that the variable "year" takes. The nearest I could come up with is</p>
  
  <pre><code>dt1 &lt;- cbind (dt1, model.matrix(~dt1$year - 1) )
  </code></pre>
  
  <p>But this has the unfortunate consequences of</p>
  
  <ol>
  <li>The dummy variables are named dt1$year2003, not just "2003", "2004" etc</li>
  <li>It seems that NA rows are omitted altogether by <code>model.matrix</code> (so the above command fails due to different lengths when NA is present in the <code>year</code> variable).</li>
  </ol>
  
  <p>Of course I can get around these problems with more code, but I like my code to be as concise as possible (within reason) so if anyone can suggest better ways to make the dummy variables I would be obliged.</p>