title: Track status change and date of change in longitudinal data
body: <p>I am trying to report chronological updates on whether participants in an ongoing study ever suffered car crash injuries. Suppose I am following a growing cohort of food delivery drivers and am tracking the severity of car crash injuries. Injuries for each crash are coded as none, minor, or severe. Any driver with a prior crash injury is excluded from entering the study. All data reflect incidents that occurred after study entry.</p>
  <p>For each calendar quarter starting from the earliest observation, I’d like to report the number of drivers who crashed but (1) never sustained an injury; (2) ever sustained only minor injuries; and (3) ever sustained at least 1 severe injury. Assume that each crash results in no more than one injury. Once a driver sustains an injury, the severity of that injury will continue to get reported unless it is replaced by a more severe injury. Each driver should only be represented once per quarter. E.g., if a driver experienced multiple types of crash injuries in the same quarter, only one occurrence of the most severe injury classification should be reported.</p>
  <p>Here is my data set:</p>
  <pre><code>df &lt;- data.frame(
    DriverID = c('A', 'A', 'A', 'A', 'A',
           'B', 'B', 'B', 'B',
           'C',
           'D', 'D', 'D',
           'E', 'E'),
    study_entry = as.Date(c('2020-01-13', NA, NA, NA, NA,
                      '2020-03-02', NA, NA, NA,
                      '2020-04-15',
                      '2020-10-08', NA, NA,
                      '2020-07-20', NA)),
    crash_date = as.Date(c(NA, '2020-03-03', '2020-05-01', '2020-07-28', '2020-11-20',
                      NA, '2020-07-10', '2020-08-19', '2020-12-25',
                      NA,
                      NA, '2020-10-10', '2020-11-23',
                      NA, '2020-09-30')),
    crash_severity = c(NA, 'N', 'M', 'S', 'M', 
                 NA, 'M', 'S', 'N',
                 NA,
                 NA, 'S', 'M',
                 NA, 'N')
    )
  </code></pre>
  <p>Visually, the data might look like:</p>
  <pre><code> |--Y1Q1--|--Y1Q2--|--Y1Q3--|--Y1Q4--|
  A *    N     M       S            M
  B      *            M   S           N
  C              *        
  D                            * S  M
  E                    *         N
  
  *: Entry into study
  N: Non-injury crash
  M: Minor-injury crash
  S: Severe-injury crash
  </code></pre>
  <p>I’d like to be able to create the table below. Note that for each reporting period, the severity of future injuries is not reflected.</p>
  <pre><code>Period  Drivers  N   M   S  
  Y1Q1      2      1   0   0
  Y1Q2      3      0   1   0
  Y1Q3      4      0   0   2
  Y1Q4      5      1   0   3
  </code></pre>