简体   繁体   中英

How to merge two data frames on common columns in R with sum of others using dplyr package

Is there a way using the "dplyr" package to intersect two data frames and sum one column. For example:

Given DF 1

Var1  Var2  Var3
1     A     5
1     B     4
2     A     5
2     B     3
2     C     4

DF 2

Var1  Var2  Var3
1     A     3
1     D     2
2     E     3
2     B     3
2     G     2

And return DF 3

Var1  Var2  Var3
1     A     8
2     B     6

how easy is that my friend?

df1 %>% left_join(df2, key = c('var1', 'var2')) %>%
mutate(sum = var2 + var3) 

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