title: Grouping data in R based on specific column values
body: <p>I have a set of data in a csv file that I need to group based on transitions of one column.  I'm new to R and I'm having trouble finding the right way to accomplish this.</p>
  
  <p>Simplified version of data:</p>
  
  <pre><code>Time    Phase    Pressure    Speed
   1        0        0.015      0
   2       25        0.015      0
   3       25        0.234      0
   4       25        0.111      0
   5        0        0.567      0
   6        0        0.876      0
   7       75        0.234      0
   8       75        0.542      0
   9       75        0.543      0
  </code></pre>
  
  <p>The length of time that phase changes state is longer than above but I shortened everything to make it readable and this pattern continues on and on.  What I'm trying to do is calculate the mean of pressure and speed for each instance where the phase is non-zero. For example, in the output from the sample above there would be two lines, one with the average of the three lines where phase is 25, and with the average of the three lines when phase is 75. It will be possible to see cases where the same numeric value of phase shows up more than once, and I need to treat each of those separately. That is, in the case where phase is <code>0, 0, 25, 25, 25, 0, 0, 0, 25, 25, 0</code>, I would need to record the first group and the second group of 25s as separate events, as well as any other non-zero groups.</p>
  
  <p>What i've tried:</p>
  
  <pre><code>`csv &lt;- read.csv("c:\\test.csv")`
  `ins &lt;- subset(csv,csv$Phase == 25)`
  `exs &lt;- subset(csv,csv$Phase == 75)`
  `mean(ins$Pressure)`
  `mean(exs$Pressure)`
  </code></pre>
  
  <p>This obviously returns the average of the entire file when phase is 25 and 75, but I need to somehow split it into groups using the trailing and leading 0s.  Any help is appreciated.</p>