title: Merge two tables if groupby argument falls in an interval (non equi join in R)
body: <p>I have the following two tables:</p>
  
  <pre><code>df &lt;- data.table(id = c("01","02","03"), tariff = c("1A","1B","1A"), summer = c(0,0,1), expenditure = c(150,200,90))
     id tariff summer expenditure
  1: 01     1A      0         150
  2: 02     1B      0         200
  3: 03     1A      1          90
  
  catalogue &lt;- data.table(tariff = c("1A","1A","1A","1A","1B","1B","1B","1B"), summer = c(0,0,1,1,0,0,1,1),
                          lb_quant = c(0,50,0,80,0,80,0,100), ub_quant = c(50,Inf,80,Inf,80,Inf,100,Inf), case = letters[1:8])
     tariff summer lb_quant ub_quant case
  1:     1A      0        0       50    a
  2:     1A      0       50      Inf    b
  3:     1A      1        0       80    c
  4:     1A      1       80      Inf    d
  5:     1B      0        0       80    e
  6:     1B      0       80      Inf    f
  7:     1B      1        0      100    g
  8:     1B      1      100      Inf    h
  </code></pre>
  
  <p>I want to merge <code>df</code> and <code>catalogue</code> by <code>tariff</code>, <code>summer</code> and <code>expenditure</code>. However, <code>expenditure</code> is numeric, so merging will not work directly.</p>
  
  <p>I'm looking for a vectorized way to merge the two tables together if:</p>
  
  <ol>
  <li><code>tariff</code> and <code>summer</code> match</li>
  <li><code>catalogue$lb_quant &lt; df$expenditure &lt;= catalogue$ub_quant</code></li>
  </ol>
  
  <p>As an example, I would like to match <code>df[id == "01"]</code> with the second line of <code>catalogue</code> because <code>tariff == "01"</code> and <code>summer == 0</code> and <code>expenditure</code> falls within <code>[50, inf)</code>. So assign <code>case = b</code> to <code>df[id = "01"]</code>.</p>
  
  <p>The real <code>df</code> is huge and I want to avoid using loops. Is there a vectorized way to achieve this in R or Python?</p>