 <p>This should precisely answer your need.<br> It uses very powerful R feature called <em>computing on the language</em> (or meta programming) well described in official <a href="https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Computing-on-the-language" rel="nofollow">R Language Definition</a> manual. This is an exceptional feature of R language and should not be forgotten IMO.</p> <p></p> <pre><code>library(data.table) DT1 = data.table(x=c("c", "a", "b", "a", "b"), a=1:5) DT2 = data.table(x=c("d", "c", "b"), b=6:8) jj = as.call(c( list(as.name(".")), list(sum = quote(a+b)), lapply(unique(c(names(DT1), names(DT2))), as.name) )) print(jj) #.(sum = a + b, x, a, b) DT1[DT2, eval(jj), on="x"] # sum x a b #1: NA d NA 6 #2: 8 c 1 7 #3: 11 b 3 8 #4: 13 b 5 8 </code></pre>