title: Conditional join in r
body: <p>I would like to conditionally join two data tables together:</p>
  
  <pre><code>library(data.table)
  set.seed(1)
  
  key.table &lt;- 
    data.table(
      out = (0:10)/10,
      keyz = sort(runif(11))
    )
  
  large.tbl &lt;- 
    data.table(
      ab = rnorm(1e6),
      cd = runif(1e6)
    )
  </code></pre>
  
  <p>according to the following rule: match the smallest value of <code>out</code> in <code>key.table</code> whose <code>keyz</code> value is larger than <code>cd</code>. I have the following:</p>
  
  <pre><code>library(dplyr)
  large.tbl %&gt;%
    rowwise %&gt;%
    mutate(out = min(key.table$out[key.table$keyz &gt; cd]))
  </code></pre>
  
  <p>which provides the correct output. The problem I have is that the <code>rowwise</code> operation seems expensive for the <code>large.tbl</code> I am actually using, crashing it unless it is on a particular computer. Are there less memory-expensive operations? The following seems slightly faster, but not enough for the problem I have.</p>
  
  <pre><code>large.tbl %&gt;%
      group_by(cd) %&gt;%
      mutate(out = min(key.table$out[key.table$keyz &gt; cd]))
  </code></pre>
  
  <p>This smells like a problem with a <code>data.table</code> answer, but the answer does not have to use that package.</p>