title: how to convert table() to matrix in r
body: <p>I have a 6x6 matrix as shown below:</p>
  
  <pre><code>M = matrix(0,nrow = 6, ncol = 6);
  for(i in 1:6)
    for(j in 1:6)
      M[i,j]&lt;- i+j
  </code></pre>
  
  <p>M is a 6x6 matrix with elements:</p>
  
  <pre><code>     [,1] [,2] [,3] [,4] [,5] [,6]
  [1,]    2    3    4    5    6    7
  [2,]    3    4    5    6    7    8
  [3,]    4    5    6    7    8    9
  [4,]    5    6    7    8    9   10
  [5,]    6    7    8    9   10   11
  [6,]    7    8    9   10   11   12
  </code></pre>
  
  <p>I want to derive a matrix of elements with their frequencies from above matrix M using r function table().</p>
  
  <p>the output of table(M) is:</p>
  
  <pre><code>&gt; table(M)
  M
   2  3  4  5  6  7  8  9 10 11 12 
   1  2  3  4  5  6  5  4  3  2  1 
  </code></pre>
  
  <p>How to simply make a 11x2 matrix from table(M) output?</p>
  
  <p>I tried as.matrix(table(M)) but it makes the elements as row names although I am not sure what's happening!</p>
  
  <pre><code>&gt; as.matrix(table(M))
     [,1]
  2     1
  3     2
  4     3
  5     4
  6     5
  7     6
  8     5
  9     4
  10    3
  11    2
  12    1
  </code></pre>
  
  <hr>
  
  <p>As suggested:</p>
  
  <pre><code>DF &lt;- data.frame(table(M))
  ma &lt;- data.matrix(DF)
  
        M Freq
   [1,]  1    1
   [2,]  2    2
   [3,]  3    3
   [4,]  4    4
   [5,]  5    5
   [6,]  6    6
   [7,]  7    5
   [8,]  8    4
   [9,]  9    3
  [10,] 10    2
  [11,] 11    1
  </code></pre>
  
  <p>1st column is changed. Each element is subtracted by 1. Why?</p>