title: How to “flatten” or “collapse” a 2D data frame into a 1D data frame in R?
body: <p>I have a two dimensional table with distances in a data.frame in R (imported from csv): </p>
  
  <pre><code>           CP000036   CP001063      CP001368
  CP000036      0           a            b
  CP001063      a           0            c
  CP001368      b           c            0
  </code></pre>
  
  <p>I'd like to "flatten" it. that I have one axes's value in the first col, and the other axes's value in the second col, and then the distance in the third col:</p>
  
  <pre><code>Genome1      Genome2       Dist
  CP000036     CP001063       a
  CP000036     CP001368       b
  CP001063     CP001368       c
  </code></pre>
  
  <p>Above is ideal, but it would be completely fine to have repetition such that each cell in the input matrix has it's own row:</p>
  
  <pre><code>Genome1      Genome2       Dist
  CP000036     CP000036       0
  CP000036     CP001063       a
  CP000036     CP001368       b
  CP001063     CP000036       a
  CP001063     CP001063       0
  CP001063     CP001368       c
  CP001368     CP000036       b
  CP001368     CP001063       c
  CP001368     CP001368       0
  </code></pre>
  
  <p>Here is an example 3x3 matrix, but my dataset I is much larger (about 2000x2000). I would do this in Excel, but I need ~3 million rows for the output, whereas Excel's maximum is ~1 million.</p>
  
  <p>This question is very similar to 
  "How to “flatten” or “collapse” a 2D Excel table into 1D?"<a href="https://stackoverflow.com/questions/687470/how-to-flatten-or-collapse-a-2d-excel-table-into-1d">1</a></p>