title: Calculate time elapsed within runs of a variable
body: <p>I have an data file with a time and output column.
  The output column consists of values 1 and 2.
  For each run of the output column where it takes the value 2, I want to compute the total time elapsed during the run, namely the end time minus the start time. For example:</p>
  
  <pre><code>    time          output       total
        2                2           4-2=2
        4                2
        6                1
        8                2           10-8=2
        10               2
        12               1
        14               1
        16               1
        18               2           22-18=4
        20               2
        22               2
  </code></pre>
  
  <p>Is there some easy way to so this for large data frames?</p>