By Dan Goldstein


2009-08-19 13:18:11 8 Comments

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, i.e., sql style, joins? That is, how do I get:

  • An inner join of df1 and df2:
    Return only the rows in which the left table have matching keys in the right table.
  • An outer join of df1 and 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
    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
    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?

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] How do you merge two Git repositories?

44 Answered Questions

27 Answered Questions

[SOLVED] What is the difference between "INNER JOIN" and "OUTER JOIN"?

22 Answered Questions

[SOLVED] LEFT OUTER JOIN in LINQ

  • 2010-08-04 11:18:21
  • Toy
  • 726838 View
  • 559 Score
  • 22 Answer
  • Tags:   c# linq join

14 Answered Questions

[SOLVED] Drop unused factor levels in a subsetted data frame

11 Answered Questions

[SOLVED] INNER JOIN ON vs WHERE clause

6 Answered Questions

[SOLVED] Difference between JOIN and INNER JOIN

20 Answered Questions

[SOLVED] Drop data frame columns by name

  • 2011-01-05 14:34:29
  • Btibert3
  • 1537780 View
  • 898 Score
  • 20 Answer
  • Tags:   r dataframe r-faq

9 Answered Questions

[SOLVED] What is the difference between Left, Right, Outer and Inner Joins?

  • 2009-01-15 19:07:31
  • MrM
  • 377265 View
  • 565 Score
  • 9 Answer
  • Tags:   sql database join

Sponsored Content