title: Creating factor-level indicator variables in R using spread or cast
body: <p>Assume a data structure like the following</p>
  
  <pre><code>MemberID &lt;- c(123,123,234,234)
  nbin &lt;- 4
  imatrix &lt;- matrix(sample(c(0,1), size=nbin * length(MemberID), replace=TRUE), 
                    nrow=length(MemberID))
  colnames(imatrix) &lt;- letters[1:nbin]
  years &lt;- c("Y1","Y2","Y1","Y2")
  
  mydf &lt;- data.frame(cbind(MemberID, years, imatrix))
  </code></pre>
  
  <p>How can I make a similar data structure such that I have an indicator for each level of years for each a,b,c,d. </p>
  
  <p>I would like to have a 2 x 9 data frame with columns MemberID, a.Y1, a.Y2, b.Y1,b.Y2,...</p>
  
  <p>Ideally, I would like to do this with <code>spread</code> or <code>cast</code> as I have been using those tools a bit and would like to learn more about how to use them.</p>