title: convert Days in occurences in R
body: <p>I have a data of this form :</p>
  <pre><code>ID    FIRM   PAY_START    PAY_end
  1      A        1         359
  1      A        360       500
  1      B        720       780
  1      C        800       930
  1      D        934       1200
  2      E        1         316
  2      E        320       360
  3      F        1         339
  3      F        340       342
  3      G        345       600
  </code></pre>
  <p>I want First to convert PAY_START( which are in Days) into occurences in the Panel 1, 2, 3 like this:</p>
  <pre><code>ID    FIRM   PAY_START    PAY_end  Occurence
  1      A        1         359      1
  1      A        360       500      2
  1      B        720       780      3
  1      C        800       930      4
  1      D        934       1200     5
  2      E        1         316      1
  2      E        320       360      2
  3      F        1         339      1
  3      F        340       342      2
  3      G        345       600      3
  </code></pre>
  <p>And I want to compute the lags of Pay_end:</p>
  <pre><code>ID    FIRM   PAY_START    PAY_end  Occurence   Lag_Pay_end
  1      A        1         359      1           0
  1      A        360       500      2           141
  1      B        720       780      3           280
  1      C        800       930      4           150
  1      D        934       1200     5           270
  2      E        1         316      1           0
  2      E        320       360      2           44
  3      F        1         339      1           0
  3      F        340       342      2           3
  3      G        345       600      3           258
  </code></pre>
  <p>Thank you for your help!</p>