简体   繁体   中英

match/merge dataframes with a number columns with different column names in r

I have two dataframe with different columns that has large number of rows (about 2 million)

The first one is df1

在此处输入图片说明

The second one is df2

在此处输入图片说明

I need to get match the values in y column from table one to R column in table two Example: see the two rows in df1 in red box have matched the two rows in df2 in red box 在此处输入图片说明

Then I need to get the score of the matched values

so the result should look like this and it should be stores in a dataframe:

在此处输入图片说明

My attempt : first Im beginner in R, so when I searched I found that I can use Match function, merge function but I did not get the result that I want it might because I did not know how to use them correctly, therefore, I need step by step very simple solution

 merge(df1,df2,by.x="y",by.y="R")[c("y","score")]
    y score
1   2     3
2 111     4

We can use match from base R

df2[match(df2$R, df1$y, nomatch = 0), c("R", "score")]
#   R score
#3   2     3
#4 111     4

Or another option is semi_join from dplyr

library(dplyr)
semi_join(df2[-1], df1, by = c(R = "y"))
#    R score
#1   2     3
#2 111     4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM