Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

How to combine 2 dataframes based on one column and have the other columns fill in [duplicate]

$
0
0

This question already has an answer here:

I am trying to combine df1 and df2 based on ID, and have the other columns just fill in according to this merge (if the column names are the same).

df1
#    ID Name   Age  Gender
#    1  Sally  40    F
#    2  Alex   30    M
#    3  Kate   50    F
#    4  Bud    35    M

df2
#    ID Name   Age  Eye_Color
#    1  Sally  40   Blue
#    2  Alex   30   Green
#    4  Bud    35   Brown

And I would like the end result to look like this:

df3
#    ID Name   Age  Gender Eye_color  
#    1  Sally  40   F      Blue
#    2  Alex   30   M      Green
#    3  Kate   50   F      NA
#    4  Bud    35   M      Brown

So far I have used both merge(df1,df2,by="ID") and full_join(df1,df2,by="ID'), but both give me columns such as Name.x and Name.y.

If I just do full_join with no 'by=' then I get the following:

df3<-full_join(df1,df2)
df3
#   ID Name   Age  Gender Eye_Color
#   1  Sally  40   F      NA
#   1  Sally  40   NA     Blue
#   2  Alex   30   M      NA
#   2  Alex   30   NA     Green
#   3  Kate   50   F      NA
#   4  Bud    35   M      NA
#   4  Bud    35   NA     Brown

Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>