0

How do I merge two (or more) dataframes based on only their shared column names?

For example:

df1 consists of ["car", "power", "age"]
df2 consists of ["car", "power", "distance"]

When df1 and df2 are being merged, the resulting df should consist of the columns:

["car", "power"]

I tried to do this via merge() but didn't work. rbind() and cbind() won't work on this and inner_join or outer_join I also couldn't get to work.

Ben
  • 1,432
  • 4
  • 20
  • 43
  • 1
    `won't work, couldn't get it to work, tried merge` doesn't mean much. Please share your code. – zx8754 May 23 '23 at 12:04
  • 1
    Provide example input and expected output. – zx8754 May 23 '23 at 12:05
  • The default behavior is to merge based on common column names. It doesn't make sense that this didn't work. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick May 23 '23 at 13:59

1 Answers1

-1

A left join will do what you want.

library(dplyr)      
left_join(df1, df2)
szmple
  • 453
  • 1
  • 8
  • Thank you, didn't find this one. Is this also possible with more than two dfs or do I have to iterate? – Ben May 23 '23 at 12:02