title: R Need to compare rows in R based on an ID and a non- Unique second column distribution of a time shift
body: <p>I need to compare the first and last value (take the difference) . I tried lagging the user (C/A)  column. Picture person 1 as C and person 2 as A. </p>
  
  <p>The issue is that there are distinct periods from C to C to C etc where there is no upper limit, so I need something flexible enough. For 2 periods I can just lug sequence and join on that , but that is simply a special case and doesn't help me at all. What I need to determine is the distribution of the time shift from the first C to the last C , for each period, so each ID may have multiple C->C-> C -> etc  shifts, So I cannot create a unique identifier by concatenating ID and Sequence and user to group by which would have been great if each period of Cs had a unique id as well. There is a Dummy for when C=C to help identify. If I can sum by the difference by dummy for each period of Cs I can get what I need too. For example below the 17+15 in rows 2 and 3 = the time difference for the C-C-C period, I need that for all occurrences. </p>
  
  <p>Here is an example of the data so far: </p>
  
  <p>Data:    </p>
  
  <pre><code> ID    Sequen_num    user      time_shift  userlag1  time_shift2  difference    Dummy(user= C userlag1=C )  
  
  1         1             A           1           C            15        14            0  
  1         2             C          15           C            32        17            1  
  1         3             C          32           C            47        15            1  
  1         4             C          47           A            65        18            0
  1         5             A          65           C            80        15            0
  1         6             C          80           C            110       30            1
  1         7             C          110          A            120       10            0
  1         8             A          120  
  </code></pre>
  
  <p>If I had something like this it would be amazingly easy(basically a way to identify each block within each ID) :</p>
  
  <pre><code>  ID    Sequen_num        user block   user      time_shift  userlag1  time_shift2  difference    Dummy(user= C userlag1=C )  
  
  1         1                1          A           1           C            15        14            0  
  1         2                2          C          15           C            32        17            1  
  1         3                2          C          32           C            47        15            1  
  1         4                2          C          47           A            65        18            0
  1         5                3          A          65           C            80        15            0
  1         6                4          C          80           C            110       30            1
  1         7                4          C          110          A            120       10            0
  1         8                5          A          120  
  </code></pre>