title: Data frame into a symmetric matrix while keeping all row and column
body: <p>The question <a href="https://stackoverflow.com/questions/29348196/r-dataframe-into-square-symmetric-matrix">R DataFrame into square (symmetric) matrix</a> is about transforming a data frame into a symmetric matrix given the following example:</p>
  
  <pre><code># data
  x &lt;- structure(c(1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0), 
                   .Dim = c(3L,5L), 
                   .Dimnames = list(c("X", "Y", "Z"), c("A", "B", "C", "D", "E")))
  
  x
  #  A B C D E
  #X 1 1 0 0 0
  #Y 1 1 0 0 0
  #Z 0 0 0 1 0
  
  # transformation
  x &lt;- x %*% t(x)
  diag(x) &lt;- 1
  
  x
  #  X Y Z
  #X 1 2 0
  #Y 2 1 0
  #Z 0 0 1
  </code></pre>
  
  <p>Now, I'm trying to keep the columns <code>c("A", "B", "C", "D", "E")</code> in the matrix, like this:</p>
  
  <pre><code>#  X Y Z A B C D E
  #X 1 0 0 1 1 0 0 0
  #Y 0 1 0 1 1 0 0 0
  #Z 0 0 1 0 0 0 1 0
  #A 1 1 0 1 0 0 0 0
  #B 1 1 0 0 1 0 0 0
  #C 0 0 0 0 0 1 0 0
  #D 0 0 1 0 0 0 1 0
  #E 0 0 0 0 0 0 0 1
  </code></pre>
  
  <p>I'm pretty sure there's an easy way to do it similar to the first transformation including each case. Can anyone suggest a solution?</p>
  
  <p>Thank you in advance!</p>