0

I have two dataframes (df1 and df2) in R that I am trying to join. Neither have the same unique column, but they do both share a name column. Here is a snippet of each data frame:

df1:

name sale_id sale_amnt product
John 03782 200 tech
Max 87367 85 clothes
Beth 63624 50 home
Max 01873 500 tech
Jeff 98077 300 home
Jeff 87321 100 clothes

df2:

name state region
John WI USA
Max CA USA
Beth CO USA
Jeff NA Canada

This is the data frame that I am trying to create:

df3:

name sale_id sale_amnt product state region
John 03782 200 tech WI USA
Max 87367 85 clothes CA USA
Beth 63624 50 home CO USA
Max 01873 500 tech WI USA
Jeff 98077 300 home NA Canada
Jeff 87321 100 clothes NA Canada

I tried using an outer join but that did not work. What should I be doing instead?

Laura
  • 15
  • 3
  • Columns don't have to be unique to join. This can be come with a standard merge/join: `merge(df1, df2)`. Since they both have a column named "name", that will be used for joining. – MrFlick Jun 29 '21 at 04:16
  • I tried merge(df1, df2) but it just returns an empty df3 with all of the headers from df1 and df2 – Laura Jun 29 '21 at 04:20
  • That that means none of the values in the `name` column matched exactly. Did you actually use the data above in the example? If we can't replicate the problem it's very difficult to help. Please share a `dput()` of your data objects rather than these tables because the `dput` will show how the data is stored in R. You can see more at [how to make a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Jun 29 '21 at 04:21
  • 1
    Thank you for the comment! There were added spaces in df2. Problem solved – Laura Jun 29 '21 at 04:28

0 Answers0