Using R, I get lost pretty quickly in conditional/if-else type work. Usually I'm able to problem solve with stack overflow threads, but I haven't figured out how to search for this specific problem. Let's say I have the following DF:
| Column A | Column B | Column C |
|---|---|---|
| Panda | Variant 1 | 10.0 |
| Monkey | Variant 2 | 5.0 |
| Monkey | Variant 1 | 7.0 |
| Panda | Variant 3 | 8.0 |
I want to make a new column, Column D. If Column B == any variant other than Variant 2, then I want the value of Column D == Column C. If Column B == "Variant 2", then I want the value of Column D == (Column C + Column C when Column A is the same but Column B is Variant 1).
So, for the above table the outcome would be:
| Column A | Column B | Column C | Column D |
|---|---|---|---|
| Panda | Variant 1 | 10.0 | 10.0 |
| Monkey | Variant 2 | 5.0 | 12.0 |
| Monkey | Variant 1 | 7.0 | 7.0 |
| Panda | Variant 3 | 8.0 | 8.0 |
I've tried a few different if/else statements to try and get the ball rolling, but none have even come close. Any solution would be greatly appreciated!
Here is an option:
Basically, you pull out those rows where "when Column A is the same" and
Column B == "Variant 2"and sum the values and then only update those rows (since the others remain the same).