title: Joining two data frames together based on quarters and years
body: <p>I have two data frames I want to join together. I have a series of <code>dates</code> and I want to join a <code>quarter</code> at time <code>t</code> up with its <code>t+1</code> <code>quarter</code>. </p>
  
  <p>I get a little stuck at quarter 4, joining up with the <code>year + 1</code>. i.e. <code>Q4</code> of <code>2006</code> should be joined with <code>Q1</code> of <code>2007</code>.</p>
  
  <p>The data I have is that I have an event which occurs once per year, say <code>February 15th 2006</code> and another event which occurs <code>March 3rd 2006</code>. At the end of March I collect all the events together and then obtain a number between <code>1</code> and <code>5</code> for each document. I want to the track the monthly performance over the next 3 months (or in quarter 2 in this case).</p>
  
  <p>Then I take the events which happened between <code>April</code> and <code>June</code> and track these from <code>July</code> - <code>Sept</code>.</p>
  
  <p>Then take all the events which happened between <code>July</code> and <code>September</code> and track the performance from <code>Oct</code> to <code>Dec</code>.</p>
  
  <p>Take all the events which happened between <code>Oct</code> and <code>Dec</code> and track the performance from <code>Jan t+1</code> to <code>Mar t+1</code>.</p>
  
  <p>How can this be done?</p>
  
  <pre><code>library(lubridate)
  
  dates_A &lt;- sample(seq(as.Date('2005/01/01'), as.Date('2010/01/01'), by="day"), 1000)
  x_var_A &lt;- rnorm(1000)
  
  d_A &lt;- data.frame(dates_A, x_var_A) %&gt;%
    mutate(quarter_A = quarter(dates_A),
           year_A = year(dates_A))
  
  
  dates_B &lt;- sample(seq(as.Date('2005/01/01'), as.Date('2010/01/01'), by="day"), 1000)
  x_var_B &lt;- rnorm(1000)
  
  d_B &lt;- data.frame(dates_B, x_var_B) %&gt;%
    mutate(quarter_B = quarter(dates_B),
           year_B = year(dates_B),
           quarter_plus_B = quarter(dates_B + months(3)))
  </code></pre>