title: R - Compare rows of data.frame (to combine based on conditions)
body: <p>I'm starting with a data.frame of genome ranges (a chromosome, and a start - end position).  I'm trying to combine rows that are 1) adjacently positioned and 2) that share a value in two other columns.  Note: I'd like an efficient method since my real data is > 10 million rows. (data.table if possible)</p>
  
  <p>Toy data:</p>
  
  <pre><code>DF &lt;- data.frame(SampleID = c(1,1,1,1,1,2,2),
                   Chr = c(1,1,1,1,2,1,1),
                   Start = c(1, 101, 201, 401, 500, 1, 101),
                   End = c(100, 200, 300, 499, 599, 100, 200),
                   State = c(3,3,2,3,3,2,2)
                   )
  DF
     SampleID Chr Start End State
  1:        1   1     1 100     3
  2:        1   1   101 200     3
  3:        1   1   201 300     2
  4:        1   1   401 499     3
  5:        1   2   500 501     3
  6:        2   1     1 100     2
  7:        2   1   101 200     2
  </code></pre>
  
  <p>Lines 1 &amp; 2 can be combined because they are adjacent (1-100 &amp; 101-200) and share a <code>SampleID</code> (1) and <code>State</code> (3). </p>
  
  <p>The following cannot be combined:</p>
  
  <ul>
  <li>Lines 2 &amp; 3 have mismatched <code>State</code>s</li>
  <li>Lines 3 &amp; 4 are not adjacent and do not share <code>State</code></li>
  <li>Lines 4 &amp; 5 differ in Chromosome (<code>Chr</code>)</li>
  <li>Lines 6 &amp; 7 are a different <code>SampleID</code>.  </li>
  </ul>
  
  <p>Etcetera.  When we apply all these, we have this final table.</p>
  
  <pre><code>FinalDF &lt;- data.frame(SampleID = c(1,1,1,1,2),
                        Chr = c(1,1,1,2,1),
                        Start = c(1,201,401,500,1),
                        End = c(200,300,499,599,200),
                        State = c(3,2,3,3,2))
  FinalDF
    SampleID Chr Start End State
  1        1   1     1 200     3
  2        1   1   201 300     2
  3        1   1   401 499     3
  4        1   2   500 599     3
  5        2   1     1 200     2
  </code></pre>
  
  <p>So, far, I've tried using the reduce function from the GenomicRanges package, but it doesn't work.</p>
  
  <p><strong>INCORRECT OUTPUT</strong></p>
  
  <pre><code>reduce(DF2)
  GRanges object with 3 ranges and 0 metadata columns:
        seqnames     ranges strand
           &lt;Rle&gt;  &lt;IRanges&gt;  &lt;Rle&gt;
    [1]        1 [  1, 300]      *
    [2]        1 [401, 499]      *
    [3]        2 [500, 501]      *
    -------
    seqinfo: 2 sequences from an unspecified genome; no seqlengths
  </code></pre>
  
  <p>I was trying to do something with data.table, since my data.frames are 10 million rows long or more, but haven't been able to figure it out.</p>
  
  <p>The following question is along the same lines (maybe a bit more complex), but has no solution.
  <a href="https://stackoverflow.com/questions/34192216/r-collapse-rows-based-on-contents-of-two-columns">R- collapse rows based on contents of two columns</a></p>