简体   繁体   中英

Which is the fastest way to compare a couple of columns between two data frames in R?

Its all about compare the group var1 and var2. Example:

df1:
index  var1  var2  col1 col2
 1       2     3     4    5
 2      10    12    15   16
 3      23    45     1   21
 4      26    18    10   16
 5      20    26    14   22

df2:
index  var1  var2  col1 col2
 1       2     3     4    5
 2     666    12    15   16
 3      23    45     1   21
 4      26   555    10   16
 5      20    26    14   22

Notice that there were just 2 changes.

result df:
index  var1  var2  col1 col2
 1       2     3     4    5
 3      23    45     1   21
 5      20    26    14   22

I think a merge or join can do what you need:

merge(df1, df2[,c("index", "var1", "var2")], by = c("index", "var1", "var2"))
#   index var1 var2 col1 col2
# 1     1    2    3    4    5
# 2     3   23   45    1   21
# 3     5   20   26   14   22

dplyr::inner_join(df1, df2[,c("index", "var1", "var2")], by = c("index", "var1", "var2"))
#   index var1 var2 col1 col2
# 1     1    2    3    4    5
# 2     3   23   45    1   21
# 3     5   20   26   14   22

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