How to join data frames in R (inner, outer, left, right)?
Posted
by Dan Goldstein
on Stack Overflow
See other posts from Stack Overflow
or by Dan Goldstein
Published on 2009-08-19T13:18:11Z
Indexed on
2010/05/03
15:38 UTC
Read the original article
Hit count: 352
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 df1
- An outer join of df1 and df2
- A left outer join of df1 and df2
- A right outer join of df1 and df2
P.S. IKT-JARQ (I Know This - Just Adding R Questions)
Extra credit:
How can I do a sql style select statement?
© Stack Overflow or respective owner