为了账号安全,请及时绑定邮箱和手机立即绑定

如何连接(合并)数据框(内部,外部,左侧,右侧)

如何连接(合并)数据框(内部,外部,左侧,右侧)

12345678_0001 2019-05-20 16:25:26
给出两个数据框:df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3)))df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1)))df1#  CustomerId Product#           1 Toaster#           2 Toaster#           3 Toaster#           4   Radio#           5   Radio#           6   Radiodf2#  CustomerId   State#           2 Alabama#           4 Alabama#           6    Ohio我怎样才能做数据库风格,即sql风格,加入?也就是说,我该怎么做:一个内连接的df1和df2:只返回行中左表在右表匹配的密钥。一个外连接的df1和df2:返回两个表中的所有行,从有右表中的匹配键左连接记录。甲左外连接(或简称为左加入)的df1和df2左表中返回所有行,并与匹配的右表键任何行。一个右外连接的df1,并df2返回右表中的所有行,任何行与左表中匹配的密钥。额外信用:如何进行SQL样式选择语句?
查看完整描述

4 回答

?
Helenr

TA贡献1780条经验 获得超3个赞


内连接有data.table方法,这非常节省时间和内存(对于一些较大的data.frames是必需的):


library(data.table)


dt1 <- data.table(df1, key = "CustomerId") 

dt2 <- data.table(df2, key = "CustomerId")


joined.dt1.dt.2 <- dt1[dt2]

merge也适用于data.tables(因为它是通用的和调用merge.data.table)


merge(dt1, dt2)

stackoverflow上记录的data.table:

如何进行data.table合并操作
将外键上的SQL连接转换为R data.table语法
为更大的data.frames合并的有效替代方案R 
如何使用data.table进行基本的左外连接在R?

另一种选择是joinplyr包中找到的功能

library(plyr)


join(df1, df2,

     type = "inner")


#   CustomerId Product   State

# 1          2 Toaster Alabama

# 2          4   Radio Alabama

# 3          6   Radio    Ohio

为选项type:inner,left,right,full。


From ?join:与merge[ join] 不同,[ ]无论使用何种连接类型,都会保留x的顺序。


查看完整回答
反对 回复 2019-05-20
?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

你也可以使用Hadley Wickham令人敬畏的dplyr软件包进行连接。


library(dplyr)


#make sure that CustomerId cols are both type numeric

#they ARE not using the provided code in question and dplyr will complain

df1$CustomerId <- as.numeric(df1$CustomerId)

df2$CustomerId <- as.numeric(df2$CustomerId)

变异连接:使用df2中的匹配将列添加到df1

#inner

inner_join(df1, df2)


#left outer

left_join(df1, df2)


#right outer

right_join(df1, df2)


#alternate right outer

left_join(df2, df1)


#full join

full_join(df1, df2)

过滤联接:过滤掉df1中的行,不要修改列

semi_join(df1, df2) #keep only observations in df1 that match in df2.

anti_join(df1, df2) #drops all observations in df1 that match in df2.


查看完整回答
反对 回复 2019-05-20
  • 4 回答
  • 0 关注
  • 983 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信