How to merge two data frames on common columns in R with sum of others?

前端 未结 3 1065
暗喜
暗喜 2020-12-02 23:02

R Version 2.11.1 32-bit on Windows 7

I got two data sets: data_A and data_B:

data_A

USER_A USER_B ACTION
1      11     0.3
1      13     0.         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 00:07

    You can use ddply in package plyr and combine it with merge:

    library(plyr)
    ddply(merge(data_A, data_B, all.x=TRUE), 
      .(USER_A, USER_B), summarise, ACTION=sum(ACTION))
    

    Notice that merge is called with the parameter all.x=TRUE - this returns all of the values in the first data.frame passed to merge, i.e. data_A:

      USER_A USER_B ACTION
    1      1     11   0.30
    2      1     13   0.25
    3      1     16   0.63
    4      1     17   0.26
    5      2     11   0.14
    6      2     14   0.28
    

提交回复
热议问题