0

I have some code in df laid out like the example, but obviously bigger. The dataframe only has 3 columns as shown below:

|quarters |source | n |
|2022 Q1  | NHS   | 5 |
|2021 Q2  | Priv  | 7 |
|2022 Q1  | Other | 9 |
|2022 Q1  | Other | 5 |

There are several 'Other' with different values. I want to collapse any 'Other' based on the quarter they are in to get a single number. E.g., the value for 2022 Q1 would be 14. I also want to delete the multiple 'Others' to keep just a single 'Other' variable for each quarter. For example, in the above DF I would want to remove the third and fourth row of the data, and have it replaced with just a single row of:

 |2022 Q1 | Other | 14 |

Anyone have any suggestions on how I can do this?

Phil
  • 7,287
  • 3
  • 36
  • 66
Snight
  • 1
  • 1
  • 2
    With the tidyverse/dplyr libraries you can do this with `df |> group_by(quarters, source) |> summarise(sum_n = sum(n))` – M.Viking Aug 10 '23 at 13:03

0 Answers0