Count event occurrences per client based on multiple reference dates
<p>I am fairly new to R, and I have a data set with client numbers and the dates of a few thousand events. The data is formatted like this:</p>

<pre><code>data &lt;- data.frame("Client"=c(rep(1, 4), rep(2, 3), rep(3, 2)), "Date"=as.Date(c("2015-11-20", "2015-12-04", "2016-01-08", "2016-04-07", "2015-12-19", "2016-02-02", "2016-02-21", "2016-01-04", "2016-02-12")), "Event"=rep(1, 9))
data
  Client       Date Event
1      1 2015-11-20     1
2      1 2015-12-04     1
3      1 2016-01-08     1
4      1 2016-04-07     1
5      2 2015-12-19     1
6      2 2016-02-02     1
7      2 2016-02-21     1
8      3 2016-01-04     1
9      3 2016-02-12     1
</code></pre>

<p>Given a set of reference dates,</p>

<pre><code> refdates &lt;- as.Date(c("2016-01-01", "2016-03-01"))
</code></pre>

<p>I would like to count the number of events per client that occurred (1) 30 days after the reference date, (2) 0-30 days before the reference date, and (3) 31-60 days before the reference date for the set of reference dates. </p>

<p>I want the output to be a data frame that looks like this:</p>

<pre><code>  Client    RefDate post30 prior30 prior31.60
1      1 2016-01-01      1       1          1
2      1 2016-03-01      0       0          1
3      2 2016-01-01      0       1          0
4      2 2016-03-01      0       2          0
5      3 2016-01-01      1       0          0
6      3 2016-03-01      0       1          1
</code></pre>

<p>I feel like I should be able to do this using plyr, but I'm feeling a bit over my head.  Can someone point me in the right direction, please?</p>