I want to add a column to my dataframe that displays frequency sums based on age group so I can calculate percentages as an additional column afterward. Right now I have two dataframes, the one I want to work with
Residential Status | age_group | frequency |
---|---|---|
1 | 50-59 | 5327 |
1 | 60-69 | 1962 |
1 | 70-79 | 224 |
1 | 80-85 | 16 |
2 | 50-59 | 1260 |
2 | 60-69 | 1176 |
2 | 70-79 | 428 |
2 | 80-85 | 75 |
... |
and the one that has the aggregate values.
age_group | group total |
---|---|
50-59 | 117812 |
60-69 | 71868 |
70-79 | 18796 |
80-85 | 6310 |
I want it to look like this:
Residential Status | age_group | frequency | group total |
---|---|---|---|
1 | 50-59 | 5327 | 117812 |
1 | 60-69 | 1962 | 71868 |
1 | 70-79 | 224 | 18796 |
1 | 80-85 | 16 | 6310 |
2 | 50-59 | 1260 | 117812 |
2 | 60-69 | 1176 | 71868 |
2 | 70-79 | 428 | 18796 |
2 | 80-85 | 75 | 6310 |
I have tried using merge(), but it's literally adding the second dataframe on top of the first. I also tried to use summarise(), but that didn't work either. Any ideas?