2021年1月1日星期五

Grouping the elements in a r data frame

I have a data frame that contains the monetary transactions among individuals. The transactions can be two-way, i.e. A can transfer money to B and B can also transfer money to A. The structure of the data frame looks like below:

From  To  Amount  A     B   $100  A     C   $40  A     D   $30  B     A   $25  B     C   $70  C     A   $190  C     D   $110  

I want to summarize the total amount of transactions among each pair of individuals who have transactions with each other and the results should be something like:

Individual_1  Individual_2    Sum  A             B               $125  A             C               $230  A             D               $30  B             C               $70  C             D               $110  

I tried to utilize the grouping feature of the package dplyr but I think it does not apply to my case. Thanks!

https://stackoverflow.com/questions/65535162/grouping-the-elements-in-a-r-data-frame January 02, 2021 at 09:03AM

没有评论:

发表评论