title: Flatten matrix in R to four columns (indexes and upper/lower triangles)
body: <p>I'm using the cor.prob() function that's been posted several times around the mailing list to get a matrix of correlations (lower diagonal) and p-values (upper diagonals):</p>
  
  <pre><code>cor.prob &lt;- function (X, dfr = nrow(X) - 2) {
    R &lt;- cor(X)
    above &lt;- row(R) &lt; col(R)
    r2 &lt;- R[above]^2
    Fstat &lt;- r2 * dfr/(1 - r2)
    R[above] &lt;- 1 - pf(Fstat, 1, dfr)
    R[row(R) == col(R)] &lt;- NA
    R
  }
  
  d &lt;- data.frame(x=1:5, y=c(10,16,8,60,80), z=c(10,9,12,2,1))
  
  cor.prob(d)
  
  &gt; cor.prob(d)
             x           y           z
  x         NA  0.04856042 0.107654038
  y  0.8807155          NA 0.003523594
  z -0.7953560 -0.97945703          NA
  </code></pre>
  
  <p>How would I collapse the above correlation matrix (with the correlations in the lower half, p-values in the upper half) into a four-column matrix: two indexes, the correlation, and the p-value? E.g.:</p>
  
  <pre><code>i  j   cor    pval
  x  y   .88    .048
  x  z  -.79    .107
  y  z  -.97  0.0035
  </code></pre>
  
  <p>I've seen <a href="https://stackoverflow.com/a/9913601/654296">the answer to the previous question like this</a>, but will only give me a 3-column matrix, not a four column matrix with separate columns for the p-value and correlation. </p>
  
  <p>Any help is appreciated!</p>