title: R: Expand a vector of matrix column numbers into a matrix with those columns filled
body: <p>I have two vectors in R and want to generate a new matrix based on them.</p>
  
  <pre><code>a=c(1,2,1,2,3)      # a[1] is 1: thus row 1, column 1 should be equal to...
  b=c(10,20,30,40,50) # ...b[1], or 10.
  </code></pre>
  
  <p>I want to produce matrix 'v' BUT without my 'for' loop through columns of v and my multiplication:</p>
  
  <pre><code>v = as.data.frame(matrix(0,nrow=length(a),ncol=length(unique(a))))
  for(i in 1:ncol(v)) v[[i]][a==i] &lt;- 1   # looping through columns of 'v'
  v &lt;- v*b
  </code></pre>
  
  <p>I am sure there is a fast/elegant way to do it in R. At least of expanding 'a' into the earlier version of 'v' (before its multiplication by 'b').</p>
  
  <p>Thanks a lot!</p>