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

左心房为你撑大大i 提交于 2020-11-11 08:21:47

问题:

Given two data frames: 给定两个数据帧:

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   Radio

df2
#  CustomerId   State
#           2 Alabama
#           4 Alabama
#           6    Ohio

How can I do database style, ie, sql style, joins ? 如何进行数据库样式(即sql样式)的联接 That is, how do I get: 也就是说,我如何获得:

  • An inner join of df1 and df2 : df1df2内部 df2
    Return only the rows in which the left table have matching keys in the right table. 仅返回右表中左表具有匹配键的行。
  • An outer join of df1 and df2 : df1df2外部 df2
    Returns all rows from both tables, join records from the left which have matching keys in the right table. 返回两个表中的所有行,左边的连接记录在右边的表中具有匹配的键。
  • A left outer join (or simply left join) of df1 and df2 df1df2 左外部联接(或简称为左 df2
    Return all rows from the left table, and any rows with matching keys from the right table. 返回左侧表中的所有行,以及右侧表中具有匹配键的所有行。
  • A right outer join of df1 and df2 df1df2 右外部连接
    Return all rows from the right table, and any rows with matching keys from the left table. 返回右侧表中的所有行,以及左侧表中具有匹配键的所有行。

Extra credit: 额外信用:

How can I do a SQL style select statement? 如何执行SQL样式选择语句?


解决方案:

参考一: https://stackoom.com/question/5S9f/如何连接-合并-数据框-内部-外部-左侧-右侧
参考二: https://oldbug.net/q/5S9f/How-to-join-merge-data-frames-inner-outer-left-right
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!