title: Create matrix from column entries
body: <p>I have the following table:</p>
  
  <pre><code>avg   m1   m2
   3    gl   fr
   1    fr   tr
   2    tr   fr
   4    la   gl
   5    na   fr
   6    tr   tr
   4    tr   fr
  </code></pre>
  
  <p>I would like to get a matrix having all the entries of m1 and m2 as row- and column names, in a specific order. It also should be symmetric. The entries in the matrix are average values of all combinations of m1 and m2. So the 2.3 in the table below is the average of fr-tr, tr-fr and tr-fr. </p>
  
  <pre><code>      gl   tr   la   na   fr
  gl    nan  nan  4    nan  3
  tr    na   6    na   nan  2.3
  la    4    nan  nan  nan  nan  
  na    nan  nan  nan  nan  5
  fr    3    2.3  nan  5    nan  
  </code></pre>
  
  <p>What I tried sofar:</p>
  
  <pre><code>matrix &lt;- acast(all, m1~m2, value.var="avg",fun.aggregate = mean)
  matrix&lt;-subset(matrix, select=c(gl,tr,la,na,fr))
  matrix&lt;-t(matrix)
  matrix&lt;-subset(matrix, select=c(gl,tr,la,na,fr))
  matrix&lt;-t(matrix)
  </code></pre>
  
  <p>That doesn't quite do what I need. It is not symmetric and it makes a distinction between the order of m1 and m2.</p>
  
  <p>Also, for a symmetric matrix, how would I make it triangular?</p>