加入导致笛卡尔

Join leads to cartesian

我有两个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('place'), 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']