Convert matrix to three defined columns in R
<p>Given <code>m</code>:</p>

<pre><code>m &lt;- structure(c(5, 1, 3, 2, 1, 4, 5, 2, 5, 1, 1, 5, 1, 4, 0, 4, 5, 
5, 3, 2, 0, 0, 3, 0, 3, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(7L, 
5L))

     # [,1] [,2] [,3] [,4] [,5]
# [1,]    5    2    0    0    0
# [2,]    1    5    4    3    0
# [3,]    3    1    5    0    0
# [4,]    2    1    5    3    0
# [5,]    1    5    3    2    0
# [6,]    4    1    2    3    0
# [7,]    5    4    0    0    0
</code></pre>

<p>Consider the element <code>1</code>, it appears in 5 rows <code>(2, 3, 4, ,5, 6)</code> and the respective column-wise indices are <code>(1, 2, 2, 1, 2)</code>. I would like to have the following:</p>

<pre><code>1 2 1
1 3 2
1 4 2
1 5 1
1 6 2
</code></pre>

<p>As another example, consider the element <code>2</code>, it appears in 4 rows <code>(1, 4, 5, 6)</code> and the respective column-wise indices are <code>(2, 1, 4, 3)</code> and we have:</p>

<pre><code>1 2 1
1 3 2
1 4 2
1 5 1
1 6 2
2 1 2
2 4 1
2 5 4
2 6 3
</code></pre>

<p>What I want is a <code>n*3</code> matrix for all 1-5. <strong>Preferably in base R</strong></p>