title: Calculate conditions on grouped cumulative sums in R
body: <p>Let's assume, that this data frame logs registrations for persons (ID)... Each person has a number of records which start on START_DT and end on END_DT.
  For each person in the data frame, I need to calculate the date when they have reached 365 days of registration.
  Some people will not achieve a year, others may have multiple records before they reach 365 days.
  Others may achieve this on their very first record (like ID = 1502).</p>
  <pre><code># DATA GENERATION
  #set seed and generate a list of 10,000 random dates
  set.seed(42)
  START_DT &lt;- sample(seq(as.Date('1910/01/01'), as.Date('2000/01/01'), by=&quot;day&quot;), 10000)
  
  #generaet a list of random numbers to add to the START DATES
  DIFF_DAYS &lt;- floor(runif(10000, min=1, max=100))
  
  #add the two
  END_DT &lt;- START_DT + DIFF_DAYS
  
  #generate person IDs
  set.seed(42)
  ID &lt;- floor(runif(10000, min=1, max=1500))
  
  #make into a dataframe
  df &lt;- data.frame(ID, START_DT, DIFF_DAYS, END_DT)
  
  #generate an additional row to ensure that at least one record surpasses 365 days in the first record.
  row &lt;- list(1502, as.Date('1910/01/01'), 2102, as.Date('1915-10-04'))
  df &lt;- rbind(df, row)
  </code></pre>
  <p>The main issue is that I have 100 billion rows, so it needs to be fast enough to churn through that.</p>
  <p>My approach so far has been to</p>
  <ol>
  <li>create a column for the cumulative counts of days (for each
  person)</li>
  <li>create another column where cumulative counts of days &lt;
  365 = 0, else, 1, thus indicating in which record the person
  achieves the 365 days.</li>
  <li>looping through, subsetting the main dataset by ID, and then calculating the result with an if/elif statement.</li>
  </ol>
  <p>I figured that this may have been a slow way of doing it... does anybody have any interesting way of solving this where it can be done as quickly as possible, please?</p>
  <p>Very grateful for any ideas as to how I can approach this problem.  Many thanks in advance :)</p>