title: Is there an R function that counts the number of previous number of dates in a data frame and based on condition
body: <p>I want to count the number of previous absences each student has had before their most recent one and add those counts as a column in the data frame.</p>
  <pre><code> Student ID       Absent Date       Subject        
  
      4567           08/30/2018          M
      4567           09/22/2019          M
      8345           09/01/2019          S
      8345           03/30/2019         PE         
      8345           07/18/2017          M
      5601           01/08/2019         SS
  </code></pre>
  <p>This is the desired output:</p>
  <pre><code> Student ID       Absent Date       Subject       Previous Absence            
  
      4567           08/30/2018          M                 1
      4567           09/22/2019          M                 1
      8345           09/01/2019          S                 2
      8345           03/30/2019         PE                 2        
      8345           07/18/2017          M                 2
      5601           01/08/2019         SS                 0
  </code></pre>
  <p>I then want to calculate the number of previous absences each student had in math (M) and add those counts as a column in the data frame.</p>
  <pre><code> Student ID       Absent Date       Subject       Previous Absence            
  
      4567           08/30/2018          M                 1
      4567           09/22/2019          M                 1
      8345           09/01/2019          S                 2
      8345           03/30/2019         PE                 2        
      8345           07/18/2017          M                 2
      5601           01/08/2019         SS                 0
  </code></pre>
  <p>The desired output:</p>
  <pre><code> Student ID  Absent Date  Subject  Prior Absence  Prior M Absence              
  
      4567      08/30/2018       M           1            1
      4567      09/22/2019       M           1            1
      8345      09/01/2019       S           2            0
      8345      03/30/2019      PE           2            0        
      8345      07/18/2017       M           2            0
      5601      01/08/2019      SS           0            0
  </code></pre>
  <p>Thank you!</p>