问题:
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
df1anddf2:df1和df2的内部df2:
Return only the rows in which the left table have matching keys in the right table. 仅返回右表中左表具有匹配键的行。 - An outer join of
df1anddf2:df1和df2的外部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
df1anddf2df1和df2左外部联接(或简称为左df2
Return all rows from the left table, and any rows with matching keys from the right table. 返回左侧表中的所有行,以及右侧表中具有匹配键的所有行。 - A right outer join of
df1anddf2df1和df2右外部连接
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
来源:oschina
链接:https://my.oschina.net/u/4432649/blog/4494520