在两个数据帧之间的列中添加和减去值

时间:2019-12-12 02:59:09

标签: r dplyr aggregate reshape mutate

我有两个数据框,如下所示:

DF1:

+-----+---------+-----+-----+
| ID  | CURRENT | JAN | FEB |
+-----+---------+-----+-----+
| 123 |       2 |   3 |   4 |
| 456 |       1 |   5 |   0 |
+-----+---------+-----+-----+

DF2:

+-----+-----------------+----------+----------+------------+
| ID  | CURRENT_2018    | JAN_2018 | FEB_2018 | UNITS_SWAP |
+-----+-----------------+----------+----------+------------+
| 123 |               5 |        6 |        7 |         12 |
| 456 |               4 |        8 |        6 |          6 |
+-----+-----------------+----------+----------+------------+

我要在此处执行的操作是从DF2中的行中依次减去UNITS_SWAP中的数字,直到UNITS_SWAP中的数字达到零为止。 同样,在执行此操作时,请将从每一行减去的UNITS_SWAP的数量添加到DF1中它们各自的匹配行中,以使两个月中相同月份的ID总数保持不变,如下所示:

DF1:

+-----+---------+-----+-----+
| ID  | CURRENT | JAN | FEB |
+-----+---------+-----+-----+
| 123 |       7 |   9 |   5 |
| 456 |       5 |   7 |   0 |
+-----+---------+-----+-----+

DF2:

+-----+-----------------+----------+----------+
| ID  | CURRENT_2018    | JAN_2018 | FEB_2018 |
+-----+-----------------+----------+----------+
| 123 |               0 |        0 |        6 |
| 456 |               4 |        6 |        6 |
+-----+-----------------+----------+----------+

按ID和月份汇总ID 123前的总数

+-------+-----------------------+---------------+---------------+
|  ID   | CURRENT, CURRENT_2018 | JAN, JAN_2018 | FEB, FEB_2018 |
+-------+-----------------------+---------------+---------------+
| 123   |                     2 |             3 |             4 |
| 123   |                     5 |             6 |             7 |
| TOTAL |                     7 |             9 |            11 |
+-------+-----------------------+---------------+---------------+

此总数应与汇总后的总数相符:

+-------+-----------------------+---------------+---------------+
|  ID   | CURRENT, CURRENT_2018 | JAN, JAN_2018 | FEB, FEB_2018 |
+-------+-----------------------+---------------+---------------+
| 123   |                     7 |             9 |             5 |
| 123   |                     0 |             0 |             6 |
| TOTAL |                     7 |             9 |            11 |
+-------+-----------------------+---------------+---------------+

类似于ID 456

之前:

+-------+-----------------------+---------------+---------------+
|  ID   | CURRENT, CURRENT_2018 | JAN, JAN_2018 | FEB, FEB_2018 |
+-------+-----------------------+---------------+---------------+
| 456   |                     1 |             5 |             0 |
| 456   |                     4 |             8 |             6 |
| TOTAL |                     5 |            13 |             6 |
+-------+-----------------------+---------------+---------------+

之后:

+-------+-----------------------+---------------+---------------+
|  ID   | CURRENT, CURRENT_2018 | JAN, JAN_2018 | FEB, FEB_2018 |
+-------+-----------------------+---------------+---------------+
| 456   |                     5 |             7 |             0 |
| 456   |                     0 |             6 |             6 |
| TOTAL |                     5 |            13 |             6 |
+-------+-----------------------+---------------+---------------+

加载数据的脚本:

DF1 <- data.frame(ID = c(123,456), 
                 CURRENT = c(2,1), 
                 JAN = c(3,5),
                 FEB=c(4,0))

DF2 <- data.frame(ID = c(123,456), 
                  CURRENT_2018 = c(4,5), 
                  JAN_2018 = c(6,8),
                  FEB_2018=c(7,6),
                  UNITS_SWAP =c(12,6))

0 个答案:

没有答案