代码之家  ›  专栏  ›  技术社区  ›  leoOrion

连接到笛卡尔

  •  0
  • leoOrion  · 技术社区  · 5 年前

    我有两个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在同一日期有多行吗?

    第二,我如何正确实现这一点?

    0 回复  |  直到 5 年前
        1
  •  1
  •   akrun    5 年前

    我们能做到

    library(data.table)
    dt1[dt2, on = .(place), rank := rank, mult = 'first']