title: R vector count number of dates within range of each date
body: <p>I am looking for the best way to implement the creation of a new variable,<code>numWithin365</code>, defined as follows:</p>
  <p><em>Given a column of dates, <code>dates</code>, count the number of other dates in the column within the preceding 365 days.</em> This problem could be generalized beyond a vector of dates.</p>
  <p>Here is one implementation; I am looking for any advice that could help it scale better.</p>
  <pre><code>library(dplyr)
  
  # set seed for reproducibility
  set.seed(42)
  
  # function to calculate number of dates in prior year
  within365 &lt;- function(col){
    sapply(col, function(x){
      sum(x-365 &lt; col &amp; col &lt;= x-1)
      }
    )
  }
  # fake data sorted chronologically
  df &lt;- data.frame(dates = sample(seq(as.Date('2015/01/01'), as.Date('2020/12/31'), 
                  by=&quot;day&quot;), 10)) %&gt;% arrange(dates)
  
  # applying the function
  df %&gt;% mutate(numWithin365 = within365(dates))
  </code></pre>
  <pre><code>        dates numWithin365
  1  2015-12-22            0
  2  2016-09-25            1
  3  2018-01-02            0
  4  2018-02-25            1
  5  2018-03-22            2
  6  2018-06-05            3
  7  2018-08-19            4
  8  2019-06-13            1
  9  2020-09-02            0
  10 2020-09-27            1
  </code></pre>