title: How can I merge two dataframes together with some conditional requirements?
body: <p>I have two dataframes, <code>df1</code> and <code>df2</code>. I would like to join the two with the following conditions:</p>
  <ol>
  <li>merge df1 and df2 on <code>gender</code> and <code>Test</code></li>
  <li><code>TestDate</code> in <code>df1</code> need to be within <code>Date1</code> and <code>Date2</code> from <code>df2</code></li>
  <li><code>all.x = TRUE</code> (keep df1 records)</li>
  </ol>
  <p>How can I handle the second part?</p>
  <p><a href="https://i.stack.imgur.com/nt928.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nt928.png" alt="Enter image description here" /></a></p>
  <pre><code>df1 &lt;- structure(list(ID = c(1, 2, 3, 5, 4), Gender = c(&quot;F&quot;, &quot;M&quot;, &quot;M&quot;, 
  &quot;F&quot;, &quot;F&quot;), TestDate = structure(c(17897, 17898, 18630, 18262, 
  17900), class = &quot;Date&quot;), Test = c(&quot;Weight&quot;, &quot;Weight&quot;, &quot;ELA&quot;, 
  &quot;ELA&quot;, &quot;Math&quot;)), row.names = c(NA, -5L), class = c(&quot;tbl_df&quot;, 
  &quot;tbl&quot;, &quot;data.frame&quot;))
  
  
  df2 &lt;- structure(list(Test = c(&quot;Weight&quot;, &quot;Weight&quot;, &quot;ELA&quot;, &quot;ELA&quot;, &quot;ELA&quot;, 
  &quot;ELA&quot;, &quot;Math&quot;, &quot;Math&quot;), Gender = c(&quot;F&quot;, &quot;M&quot;, &quot;F&quot;, &quot;M&quot;, &quot;F&quot;, &quot;M&quot;, 
  &quot;F&quot;, &quot;M&quot;), Date1 = structure(c(17532, 17534, 17536, 17537, 18266, 
  18267, 17897, 17539), class = &quot;Date&quot;), Ave = c(97, 99, 85, 84, 
  83, 82, 88, 89), Date2 = structure(c(18993, 18995, 18266, 18267, 
  18997, 18998, 18999, 19000), class = &quot;Date&quot;)), row.names = c(NA, 
  -8L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>