title: Calculate the difference betwen pairs of consecutive rows in a data frame - R
body: <p>I have a data.frame in which each gene name is repeated and contains values for 2 conditions: </p>
  
  <pre><code>df &lt;- data.frame(gene=c("A","A","B","B","C","C"),
  condition=c("control","treatment","control","treatment","control","treatment"),
  count=c(10, 2, 5, 8, 5, 1), 
  sd=c(1, 0.2, 0.1, 2, 0.8, 0.1))
  
    gene condition count  sd
  1    A   control    10 1.0
  2    A treatment     2 0.2
  3    B   control     5 0.1
  4    B treatment     8 2.0
  5    C   control     5 0.8
  6    C treatment     1 0.1
  </code></pre>
  
  <p>I want to calculate if there is an increase or decrease in "count" after treatment and mark them as such and/or subset them. That is (pseudo code):</p>
  
  <pre><code>for each unique(gene) do 
     if df[geneRow1,3]-df[geneRow2,3] &gt; 0 then gene is "up"
         else gene is "down"
  </code></pre>
  
  <p>This what it should look like in the end (the last columns is optional):</p>
  
  <pre><code>up-regulated
   gene condition count  sd  regulation
   B    control     5    0.1    up
   B    treatment   8    2.0    up
  
  down-regulated
   gene condition count  sd  regulation
   A    control     10   1.0    down
   A    treatment   2    0.2    down
   C    control     5    0.8    down
   C    treatment   1    0.1    down
  </code></pre>
  
  <p>I have been raking my brain with this, including playing with ddply, and I've failed to find a solution - please a hapless biologist. </p>
  
  <p>Cheers.</p>