Merging data frames in R
Merging data frames in R is just one of the things you can do in R. I often find myself searching online for these simple commands to combine two data frames together, either by joining or by adding rows from one data frame to another. I thought it would be a great idea to write a blog post about these commands for easy access.
Inner join:
merge(df1, df2)
or
merge(df1, df2, by = "CustomerId")
Outer join:
merge(x = df1, y = df2, by = "CustomerId", all = TRUE)
Left outer join:
merge(x = df1, y = df2, by = "CustomerId", all.x = TRUE)
Right outer join:
merge(x = df1, y = df2, by = "CustomerId", all.y = TRUE)
Cross join:
merge(x = df1, y = df2, by = NULL)
Append rows from one data frame to another:
df3 = bind_rows(df1,df2)
There are many other ways to merge data frames with each other in R but these were the easiest ways that I have found.
References:
https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right