Q:

Merge column to data.table in function

I need to add a column to a huge data.table dt.1 (from a lookup table dt.2). It can be done like this:
df.1 <- cbind(c(1,2,3,1,2,3,1,2,3),c(1,2,3,1,2,3,1,2,3),c(1,2,3,4,5,6,7,8,9))
colnames(df.1) <- c("ColA", "ColB", "ColC")
df.2 <- cbind(c(1,3),c(1,3),c(10,11))
colnames(df.2) <- c("ColA", "ColB", "ColD")
dt.1 <- data.table(df.1)
dt.2 <- data.table(df.2)

getAnotherColumn <- function() {
  keycols <- c("ColA", "ColB")
  setkeyv(dt.1, keycols)
  setkeyv(dt.2, keycols)
  dt.1 <- merge(dt.1, dt.2, all=TRUE)
  dt.1  # Will print with ColA, ColB, ColC, and ColD. As needed.
}

getAnotherColumn()
dt.1  # Only ColA, ColB, and ColC are here. ColD is also needed.

The problem is that I must do it inside a function, so at the return from the function, the old dt.1 is still there (without the new column ColD).
How can I add column to dt.1 instead of creating a new dt.1?
As dt.1 is huge, I cannot afford to make "extra copies" of it.

A:

Notice that inside the function, you are changing the object dt.1 during this line: 
   dt.1 <- merge(dt.1, dt.2, all=TRUE)

You are assigning a completely new value to the object dt.1.  It just so happens that the new value is related to the previous value of dt.1, but this is no different than, say,
   dt.1 <- "spaghetti"  # or any other unrelated value

To assign by reference, you need to use :=
getAnotherColumn <- function() {
  keycols <- c("ColA", "ColB")
  setkeyv(dt.1, keycols)
  setkeyv(dt.2, keycols)

  dt.1[dt.2, ColD := ColD]
}

getAnotherColumn()

Now ColD is in dt.1
> dt.1 
   ColA ColB ColC ColD
1:    1    1    1   10
2:    1    1    4   10
3:    1    1    7   10
4:    2    2    2   NA
5:    2    2    5   NA
6:    2    2    8   NA
7:    3    3    3   11
8:    3    3    6   11
9:    3    3    9   11

