我有两个DT。我想将DT1与基于列的DT2连接起来,并从DT2中获取一列。
DT1:
id place
1: id1 A
2: id2 B
3: id3 B
4: id4 C
5: id5 C
DT2:
place rank
1: A 3
2: B 2
3: C 1
4: D 3
5: E 2
Expected:
id place rank
1: id1 A 3
2: id2 B 2
3: id3 B 2
4: id4 C 1
5: id5 C 1
现在,我已经试过了-
dt1[dt2,on=c("位置"(,nomatch=0]
我以为这会根据place
列中的值映射所有行,并将rank
列添加到其中。但我收到一个错误,说发生了笛卡尔。
Error in vecseq(f__, len__, if (allow.cartesian || notjoin || !anyDuplicated(f__, :
Join results in <> rows; more than <> = nrow(x)+nrow(i). Check for duplicate key values in i each of which join to the same group in x over and over again. If that's ok, try by=.EACHI to run j for each group to avoid the large al
location. If you are sure you wish to proceed, rerun with allow.cartesian=TRUE. Otherwise, please search for this error message in the FAQ, Wiki, Stack Overflow and data.table issue tracker for advice.
我的第一个问题是,我不明白笛卡尔是如何在这里形成的?是因为我的第一个DT在同一日期有多行吗?
第二-我如何正确实现这一点?
我还尝试了右外部联接,而不是内部联接。我也犯了同样的错误。
我们可以进行
library(data.table)
dt1[dt2, on = .(place), rank := rank, mult = 'first']