我有两个数据框(实际上是data.tables)。
set.seed(123)
dt1 <- data.table(P=rep(letters[1:3],c(4,2,3)),X=sample(9))
dt1
P X
1: a 3
2: a 7
3: a 9
4: a 6
5: b 5
6: b 1
7: c 2
8: c 8
9: c 4
和:
dt2 <- data.table(P=rep(letters[1:5],length=10),D=c("X","Y","Z","G","F"))
dt2
P D
1: a X
2: b Y
3: c Z
4: d G
5: e F
6: a X
7: b Y
8: c Z
9: d G
10: e F
现在,我想向dt1中添加新列,其中dt2的列“ D”中P在dt1和dt2中具有相同的值。它看起来应该像这样:
dt_new
P X D
1: a 3 X
2: a 7 X
3: a 9 X
4: a 6 X
5: b 5 Y
6: b 1 Y
7: c 2 Z
8: c 8 Z
9: c 4 Z
最佳答案
我将以这种方式执行data.table join
:
setkey(dt1, P)
dt1[unique(dt2),nomatch=0]
P X D
1: a 3 X
2: a 7 X
3: a 9 X
4: a 6 X
5: b 5 Y
6: b 1 Y
7: c 2 Z
8: c 8 Z
9: c 4 Z