title: R filter a large dataframe conditional on dates, using filter and pipes
body: <p>I have a large dataframe and I want to select rows which satisfy condition on date columns. The dataframe is similar to this:</p>
  <pre><code>library(tidyverse)
  library(lubridate)
  
  curdate &lt;- seq(as.Date(&quot;2000/1/1&quot;), by = &quot;month&quot;, length.out = 24)
  expdate &lt;- rep(seq(as.Date(&quot;2000/3/1&quot;), by = &quot;quarter&quot;, length.out = 12),2)
  afactor &lt;- rep(c(&quot;C&quot;,&quot;P&quot;),12)
  anumber &lt;- runif(24)
  df&lt;-data.frame(curdate, expdate, afactor, anumber)
  df$expdate[12]&lt;-as.Date(&quot;2001-02-01&quot;)
  </code></pre>
  <p>I would like to get the rows which the month of the expiration date (expdate) is two months later than the month of current date (curdate). In this example, I should select these five dates (rows 1, 7, 12, 13 and 19):</p>
  <pre><code>curdate     expdate     afactor     anumber
  2000-01-01  2000-03-01     C        0.6832251
  2000-07-01  2001-09-01     C        0.2671076
  2001-01-01  2000-03-01     C        0.2097065
  2001-07-01  2001-09-01     C        0.9258450
  2000-12-01  2001-02-01     P        0.4903951
  </code></pre>
  <p>First I used the following line for that:</p>
  <pre><code>df_select1 &lt;- df %&gt;% group_by(curdate, afactor) %&gt;% 
    filter(month(expdate) == month(curdate)+2)
  </code></pre>
  <p>But it misses the cases when the month is November or December. For instance here, it misses the case when curdate is 2000-12-01. So I want to add a condition, to deal with these cases. I wrote:</p>
  <pre><code> df_select2 &lt;- df %&gt;% group_by(curdate, afactor) %&gt;% 
    if_else(month(curdate)&lt;11,
      filter(month(expdate) == month(curdate)+2),
      filter(month(expdate) == month(curdate)-10))
  </code></pre>
  <p>but I get the following error: <em><code>condition</code> must be a logical vector, not a <code>grouped_df/tbl_df/tbl/data.frame</code> object</em>.</p>
  <p>I found the following solution, but there are certainly much shorter ways to do it:</p>
  <pre><code>df_select1 &lt;- df %&gt;% group_by(curdate, afactor) %&gt;% 
    filter(month(curdate)&lt;11) %&gt;%
    filter(month(expdate) == month(curdate)+2)
  
  df_select2 &lt;- df %&gt;% group_by(curdate, afactor) %&gt;% 
    filter(month(curdate)&gt;10) %&gt;%
    filter(month(expdate) == month(curdate)-10)
  
  df_select &lt;- full_join(df_select1, df_select2)
  </code></pre>