数据表的简写连接异常

时间:2018-07-10 12:22:11

标签: r join merge duplicates data.table

有关R中数据表的快捷连接,请参考this

DT1 = as.data.table(data.frame(col1 = c(1,2,3,2,5,1,3,3,1,2), col2 = c(3,4,5,4,3,4,5,3,4,5), col3 = c(1,2,3,4,5,6,7,8,9,10)))
DT2 = as.data.table(data.frame(col1 = c(1,2,1,2,3,4,3,4,3), col2 = c(3,4,5,3,6,4,5,3,4), col3=c(11,12,13,14,15,16,17,19,20)))
setkey(DT1, col1, col2)
setkey(DT2, col1, col2)

DT1[DT2, col4 := DT2$col3]
DT1
DT2

Warning message:
In `[.data.table`(DT1, DT2, `:=`(col4, DT2$col3)) :
  Supplied 9 items to be assigned to 11 items of column 'col4' (recycled leaving remainder of 2 items).

结果是

DT1
    col1 col2 col3 col4
 1:    1    3    1   11
 2:    1    4    6   NA
 3:    1    4    9   NA
 4:    2    4    2   12
 5:    2    4    4   20
 6:    2    5   10   NA
 7:    3    3    8   NA
 8:    3    5    3   15
 9:    3    5    7   19
10:    5    3    5   NA

DT2
   col1 col2 col3
1:    1    3   11
2:    1    5   13
3:    2    3   14
4:    2    4   12
5:    3    4   20
6:    3    5   17
7:    3    6   15
8:    4    3   19
9:    4    4   16

发生了什么事?为什么会出现错误而结果不正确,例如DT1中的第4行和第5行?

0 个答案:

没有答案