title: Convert a data frame to matrix using indexing
body: <p>I have a data frame like below:</p>
  
  <pre><code>df&lt;- read.table(text = "A   B   C
  a1  abcd    1
  b2  abcd    -1
  c2  efgh    1
  v4  uh  1
  g5  vghvb   0
  ",header=T)
  </code></pre>
  
  <p>and I want to convert to a matrix like this:</p>
  
  <pre><code>    abcd    abcd    efgh    uh  vghvb
  a1  1   0   0   0   0
  b2  -1  0   0   0   0
  c2  0   0   1   1   0
  v4  0   0   0   0   0
  g5  0   0   0   0   1
  </code></pre>
  
  <p>I used these codes but they did not work:</p>
  
  <pre><code># get names for row and columns
  nameVals &lt;- sort(unique(unlist(df[1:2])))
  # construct 0 matrix of correct dimensions with row and column names
  myMat &lt;- matrix(0, length(nameVals), length(nameVals), dimnames = list(nameVals, nameVals))
  
  # fill in the matrix with matrix indexing on row and column names
  myMat[as.matrix(df[c("A", "B")])] &lt;- df[["C"]]
  </code></pre>
  
  <p>But no correct result, any idea?</p>