使用data.table左联接 [英] Left join using data.table

查看:87
本文介绍了使用data.table左联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个 data.table :

A:

  A  B
1: 1 12
2: 2 13
3: 3 14
4: 4 15

B:

   A  B
1: 2 13
2: 3 14

,我有以下代码:

merge_test = merge(dataA, dataB, by="A", all.data=TRUE)

我得到:

   A B.x B.y
1: 2  13  13
2: 3  14  14

但是,我希望最终合并表中的dataA中的所有行.有办法吗?

However, I want all the rows in dataA in the final merged table. Is there a way to do this?

推荐答案

您可以尝试以下操作:

# used data
# set the key in 'B' to the column which you use to join
A <- data.table(a = 1:4, b = 12:15)
B <- data.table(a = 2:3, b = 13:14, key = 'a') 

B[A]

这篇关于使用data.table左联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆