Data:
library(data.table)
A <- data.table(id = letters[1:10], amount = rnorm(10)^2)
B2 <- data.table(
id = c("c", "d", "e", "e"),
ord = 1:4,
comment = c("big", "slow", "nice", "nooice")
)
I'm trying to left-join by reference using data.table following this solution:
A[B2, on = .(id), names(B2)[2:3] := mget(paste0("i.", names(B2)[2:3]))]
Which results in the following output:
id amount ord comment
a 0.10210291 NA NA
b 0.01255382 NA NA
c 0.83172798 1 big
d 0.18312460 2 slow
e 0.98596235 4 nooice
f 0.78437310 NA NA
g 6.34467810 NA NA
h 1.12852702 NA NA
i 0.23695322 NA NA
j 0.48943532 NA NA
There is a duplicate "e" in the B2 data.table so I expected an extra row in the final output which I do get when I use left_join from dplyr (ignore the difference in the random numbers in the "amount" column):
left_join(A, B2, by = "id")
id amount ord comment
a 0.4778922 NA NA
b 1.4659516 NA NA
c 0.7857094 1 big
d 0.6697439 2 slow
e 0.2903246 3 nice <-
e 0.2903246 4 nooice
f 6.8514519 NA NA
g 1.7866884 NA NA
h 0.9687253 NA NA
i 0.7872538 NA NA
j 2.0517777 NA NA
How do I produce the same output via data.table by reference?