【发布时间】:2021-01-08 01:41:26
【问题描述】:
我有以下数据,并希望在 R 中使用 dplyr 创建如下所示的“Final Col”。
请注意,“树木”主类别在 2017 年第 1 周的值为 0。在这种情况下,我希望这两个星期的最终值为 0。
我会很感激你的想法。
| Year | Week | MainCat|Qty |Final Col |
|:----: |:------: |:-----: |:-----:|:------------:|
| 2017 | 1 | Edible |69 |69/(69+12) |
| 2017 | 2 | Edible |12 |12/(69+12) |
| 2017 | 1 | Trees |00 |00 |
| 2017 | 2 | Trees |12 |00 |
| 2017 | 1 | Flowers|88 |88/(88+47) |
| 2017 | 2 | Flowers|47 |47/(88+47) |
| 2018 | 1 | Edible |90 |90/(90+35) |
| 2018 | 2 | Edible |35 |35/(90+35) |
| 2018 | 1 | Trees |32 |32/(32+12) |
| 2018 | 2 | Trees |12 |12/(32+12) |
| 2018 | 1 | Flowers|78 |78/(78+85) |
| 2018 | 2 | Flowers|85 |85/(78+85) |
【问题讨论】: