title: Create new dummy variable columns from categorical variable
body: <p>I have a several data sets with 75,000 observations and a <code>type</code> variable that can take on a value 0-4.  I want to add five new dummy variables to each data set for all types.  The best way I could come up with to do this is as follows:</p>
  
  <pre><code># For the 'binom' data set create dummy variables for all types in all data sets
  binom.dummy.list&lt;-list()
  for(i in 0:4){
      binom.dummy.list[[i+1]]&lt;-sapply(binom$type,function(t) ifelse(t==i,1,0))
  }
  
  # Add and merge data
  binom.dummy.df&lt;-as.data.frame(do.call("cbind",binom.dummy.list))
  binom.dummy.df&lt;-transform(binom.dummy.df,id=1:nrow(binom))
  binom&lt;-merge(binom,binom.dummy.df,by="id")
  </code></pre>
  
  <p>While this works, it is incredibly slow (the merge function has even crashed a few times). Is there a more efficient way to do this?  Perhaps this functionality is part of a package that I am not familiar with?</p>