How to use `match` function for multiple column of 2 dataframe

I have 2 dataframe namely df_1 and df_2. The structures of the dataframe are given below

Dataframe 1 contains 3 columns
name_1 name_2 description
A B Good
A C Good
D C Bad

Dataframe 2 contains 3 columns
name_1 name_2 score
B F 0.05
D Y 0.06
A C 0.87

My expected dataframe is
name_1 name_2 description score
A B Good n/a
A C Good 0.87
D C Bad n/a

Now, I want to add a column score from df_2 to df_1 if the name of both columns matches. Condition looks like this

if ((df_1$name_1 == df_2$name_1) && (df_1$name_2 == df_2$name_2)) then I would like to take the score from df_2

I am using the match function but getting the wrong answer. My code is given below

df_1$score <- df_2$score[match(df_1$name_1, df_2$name_1) && match(df_1$name_2, df_2$name_2]

Could you tell me, how can I use the condition for getting the expected output?

If you use dplyr package which has left_join() function, its very easy

@nirgrahamuk Thank you.

Your suggestion worked!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.