title: Count event types over time series by multiple conditions
body: <p>I'm hoping to calculate count/sum of each subgroup within groups over a time series.</p>
  
  <p>My question is very similar to this question
  <a href="https://stackoverflow.com/questions/40369820/rolling-count-of-events-over-time-series?answertab=votes#tab-top">Rolling Count of Events Over Time Series</a>. </p>
  
  <p>Apologies for cross-posting, I have been looking for ways to count events for each category in group 1 within a time range (Present date and the previous N (say 4) days). I want to repeat this process for every subtype in group 2, i.e. Group 2 is a larger group that may/may not contains all the categories within Group 1.</p>
  
  <p>For example, if we have a data frame that looks like the following </p>
  
  <pre><code>dates = as.Date(c("2011-10-09",
      "2011-10-15",
      "2011-10-16", 
      "2011-10-18", 
      "2011-10-21", 
      "2011-10-22", 
      "2011-10-24")) 
  group1=c("A",
       "A",
       "A", 
       "A", 
       "L", 
       "L", 
       "A")
  group2=c("I",
       "I",
       "I", 
       "I", 
       "I", 
       "I", 
       "II")
  
  df1 &lt;- data.frame(dates, group1, group2) 
  </code></pre>
  
  <p>And I'm looking for output similar to this. (Edited) Eventually, I want to spread my dataset so that I will have categories in Group 1 in separate columns, and arrange rows according to dates and Group 2. How can I make sure the count of Group 1 categories is carried forward to the new row (and satisfy the timeframe stated above)? </p>
  
  <pre><code>            dates  group1 group2  count (A)   count (L)
       1 2011-10-09      A      I        1         0
       2 2011-10-15      A      I        1         0
       3 2011-10-16      A      I        2         0
       4 2011-10-18      A      I        3         0
       5 2011-10-21      L      I        0         1
       6 2011-10-22      L      I        0         2
       7 2011-10-24      A      II       1         0
  </code></pre>
  
  <p>Thanks!</p>