R语言 如何使用data.table进行完全连接?

jexiocij  于 2023-02-27  发布在  其他
关注(0)|答案(3)|浏览(206)

data.table FAQ中,nomatch = NA参数被认为类似于外部连接,但是,我还不能让data.table执行 * full * 外部连接--只能执行右外部连接。
例如:

a <- data.table("dog" = c(8:12), "cat" = c(15:19))

   dog cat
1:   8  15
2:   9  16
3:  10  17
4:  11  18
5:  12  19

b <- data.table("dog" = 1:10, "bullfrog" = 11:20)

    dog bullfrog
 1:   1       11
 2:   2       12
 3:   3       13
 4:   4       14
 5:   5       15
 6:   6       16
 7:   7       17
 8:   8       18
 9:   9       19
10:  10       20

setkey(a, dog)
setkey(b, dog)

a[b, nomatch = NA]

    dog cat bullfrog
 1:   1  NA       11
 2:   2  NA       12
 3:   3  NA       13
 4:   4  NA       14
 5:   5  NA       15
 6:   6  NA       16
 7:   7  NA       17
 8:   8  15       18
 9:   9  16       19
10:  10  17       20

因此,nomatch = NA生成一个右外连接(这是默认的)。如果我需要一个完全连接呢?例如:

merge(a, b, by = "dog", all = TRUE) 
# Or with plyr:
join(a, b, by = "dog", type = "full")

    dog cat bullfrog
 1:   1  NA       11
 2:   2  NA       12
 3:   3  NA       13
 4:   4  NA       14
 5:   5  NA       15
 6:   6  NA       16
 7:   7  NA       17
 8:   8  15       18
 9:   9  16       19
10:  10  17       20
11:  11  18       NA
12:  12  19       NA

这在data.table上可能吗?

egdjgwm8

egdjgwm81#

实际上,它就在那里。使用merge.data.table,这正是您调用时所做的

merge(a, b, by = "dog", all = TRUE)

由于adata.table,因此merge(a, b, ...)调用merge.data.table(a, b, ...)

hjzp0vay

hjzp0vay2#

x= data.table(a=1:5,b=11:15)
y= data.table(a=c(1:4,6),c=c(101:104,106))

setkey(x,a)
setkey(y,a)

unique_keys <- unique(c(x[,a], y[,a]))
y[x[.(unique_keys), on="a"]  ] # Full Outer Join
vc9ivgsu

vc9ivgsu3#

获取完全联接的另一种方法是将完全联接读作右联接加反联接:

rbind(
  fill = TRUE,
  a[b, on = ...],
  a[!b, on = ...]
)

相关问题