I have the following table:
date <- c("2021-12-30", "2021-12-30", "2021-12-30", "2022-01-06", "2022-01-06", "2022-01-06", "2022-01-13", "2022-01-13", "2022-01-13")
age_group <- c("ALL", "<60", "60+", "ALL", "<60", "60+", "ALL", "<60", "60+")
number <- c(583, 2207, 56, 662, 2852, 71, 432, 1243, 60)
number2 <- c(852, 900, 50, 1040, 1103, 76, 456, 461, 29)
data.frame(date, age_group, number, number2)
date age_group number number2
1 2021-12-30 ALL 583 852
2 2021-12-30 <60 2207 900
3 2021-12-30 60+ 56 50
4 2022-01-06 ALL 662 1040
5 2022-01-06 <60 2852 1103
6 2022-01-06 60+ 71 76
7 2022-01-13 ALL 432 456
8 2022-01-13 <60 1243 461
9 2022-01-13 60+ 60 29
As you can see, the values of "<60" and "60+" do not correspond to the value of "ALL" (i.e., for "2021-12-30" and the "number" column -> 2207 (from age group "<60") + 56 (from age group "60+") != 583 (from age group "ALL").
I want to modify the dataframe so that the values from "ALL" are equal to the sum of "<60" and "60+" for each date and each column, i.e.:
date age_group number number2
1 2021-12-30 ALL 2263 950
2 2021-12-30 <60 2207 900
3 2021-12-30 60+ 56 50
4 2022-01-06 ALL 2923 1179
5 2022-01-06 <60 2852 1103
6 2022-01-06 60+ 71 76
7 2022-01-13 ALL 1303 480
8 2022-01-13 <60 1243 461
9 2022-01-13 60+ 60 29
Is there a straightforward solution using dplyr or does this require multiple pivot_longer and pivot_wider back and forth?