I need to aggregate rows of a dataset to collapse age ranges. My dataset currently has 5-year age ranges. I'm trying to combine these age ranges into categories while summing some of the variables (Population, X1, X2, X3, and X4), while keeping the variable "Category" which is the same for each row within that specific ID.
My dataset looks like this:
ID Age.Range Population X1 X2 X3 X4 Category
1 05-09 years 10 1 0 0 1 a
1 10-14 years 20 0 0 1 0 a
1 30-34 years 10 0 0 1 0 a
1 40-44 years 15 2 0 0 1 a
2 05-09 years 15 1 1 0 2 b
2 25-29 years 10 0 0 0 0 b
3 10-14 years 15 0 1 2 0 a
3 15-19 years 10 1 0 0 1 a
3 20-24 years 15 0 0 1 3 a
3 30-34 years 20 0 0 1 0 a
3 35-39 years 10 0 1 0 0 a
I'm trying to produce a new dataframe that combines ages so that mynew age ranges are 05-29 years, 30-39 years, and 40-49 years, so it would look like this:
ID Age.Range Population X1 X2 X3 X4 Category
1 05-29 years 30 1 0 1 1 a
1 30-39 years 10 0 0 1 0 a
1 40-49 years 15 2 0 0 1 a
2 05-29 years 25 1 1 0 2 a
3 05-29 years 40 1 1 3 4 a
3 30-39 years 30 0 1 1 0 a
I've tried doing this with dplyr to no success. Any help would be appreciated!