Joining data.tables within a function
<p>I would like to change a data.table by doing a join within a function. I understand that data.tables work by reference, so assumed that reassigning a joined version of a data.table to itself would change the original data.table. What simple thing have I misunderstood?</p>

<p>Thanks!</p>

<pre><code>library('data.table')

# function to restrict DT to subset, by join
join_test &lt;- function(DT) {
    test_dt     = data.table(a = c('a', 'b'), c = c('x', 'y'))
    setkey(test_dt, 'a')
    setkey(DT, 'a')

    DT  &lt;- DT[test_dt]
}

DT  = data.table(a = c("a","b","c"), b = 1:3)
print(DT)
#    a b
# 1: a 1
# 2: b 2
# 3: c 3
haskey(DT)
# [1] FALSE

join_test(DT)
print(DT)
#    a b
# 1: a 1
# 2: b 2
# 3: c 3
haskey(DT)
# [1] TRUE
</code></pre>

<p>(haskey calls included just to double-check that some of the by reference changes work)</p>