How to join or merge data frames (inner, outer, left, right) in R

pandas provide various facilities for easily combining together Series, DataFrame, and Panel objects with various kinds of set logic to merge data frames for the indexes and relational algebra functionality in the case of join / merge-type operations.

We will create two data frame df1 and df2 to illustrate joins in R.

#df1

#df2

By using the merge function and its optional parameters:
Inner join: merge(df1, df2) will work for these examples because R automatically joins the frames by common variable names, but you would most likely want to specify to

make sure that you were matching on only the fields you desired. You can also use the by.xand parametersby.y if the matching variables have different names in the different data frames.

Outer join: merge(x = df1, y = df2, by = "CustomerId", all = TRUE)
Left outer: merge(x = df1, y = df2, by = "CustomerId", all.x = TRUE)
Right outer: merge(x = df1, y = df2, by = "CustomerId", all.y = TRUE)
Cross join: merge(x = df1, y = df2, by = NULL)

The same can also be achieved using the sqldf package in R, But using Merge is much easier and understandable.

You might also like More from author