联接导致笛卡尔

问题描述

我有两个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']