数据:
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")
)
我正在尝试使用this solution之后的data.table通过引用进行左连接:
A[B2, on = .(id), names(B2)[2:3] := mget(paste0("i.", names(B2)[2:3]))]
这将产生以下输出:
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
B2 data.table中有一个重复的“ e”,因此我期望最终输出中有一个额外的行,当我使用dplyr的left_join时,我会得到该行(忽略“金额”列中随机数的差异) ):
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
如何通过data.table通过引用产生相同的输出?