title: Data Table merge based on date ranges
body: <p>I have two tables, <code>policies</code> and <code>claims</code></p>
  
  <pre><code>policies&lt;-data.table(policyNumber=c(123,123,124,125), 
                  EFDT=as.Date(c("2012-1-1","2013-1-1","2013-1-1","2013-2-1")), 
                  EXDT=as.Date(c("2013-1-1","2014-1-1","2014-1-1","2014-2-1")))
  &gt; policies
     policyNumber       EFDT       EXDT
  1:          123 2012-01-01 2013-01-01
  2:          123 2013-01-01 2014-01-01
  3:          124 2013-01-01 2014-01-01
  4:          125 2013-02-01 2014-02-01
  
  
  claims&lt;-data.table(claimNumber=c(1,2,3,4), 
                     policyNumber=c(123,123,123,124),
                     lossDate=as.Date(c("2012-2-1","2012-8-15","2013-1-1","2013-10-31")),
                     claimAmount=c(10,20,20,15))
  &gt; claims
     claimNumber policyNumber   lossDate claimAmount
  1:           1          123 2012-02-01          10
  2:           2          123 2012-08-15          20
  3:           3          123 2013-01-01          20
  4:           4          124 2013-10-31          15
  </code></pre>
  
  <p>The policy table really contains policy-terms, since each row is uniquely identified by a policy number along with an effective date.</p>
  
  <p>I want to merge the two tables in a way that associates claims with policy-terms.  A claim is associated with a policy term if it has the same policy number and the lossDate of the claim falls within the effective date and expiration date of the policy-term (effective dates are inclusive bounds and expiration dates are exclusive bounds.)  How do I merge the tables in this way?</p>
  
  <p>This should be similar to a left outer join. The result should look like</p>
  
  <pre><code>   policyNumber       EFDT       EXDT claimNumber   lossDate claimAmount
  1:          123 2012-01-01 2013-01-01           1 2012-02-01          10
  2:          123 2012-01-01 2013-01-01           2 2012-08-15          20
  3:          123 2013-01-01 2014-01-01           3 2013-01-01          20
  4:          124 2013-01-01 2014-01-01           4 2013-10-31          15
  5:          125 2013-02-01 2014-02-01          NA       &lt;NA&gt;          NA
  </code></pre>