I am trying to sort my data into multiple columns, based on other column values.
The code I am using to get one group is:
df_grouped <- df %>%
group_by(region) %>%
filter(age > 10, age < 15) %>%
summarise(population10-15 = sum(population))
What I want is to get multiple groups:
df_grouped <- df %>%
group_by(region) %>%
mutate(population1 = filter(age > 0, age < 5)) %>%
mutate(population2 = filter(age >= 5, age < 10)) %>%
mutate(population3 = filter(age > =10, age < 15)) %>%
summarise(population = sum(population))
Hope this makes sense, my DF is a list of multiple regions with age (0-55) and population(total number)
ie
region age population
a - 1 - 183
a - 2 - 218
b - 1 - 153
b - 2 - 128
c - 1 - 67
c - 2 - 35
(but 500 regions and age going from 0-55)
I also tried case_when, but couldn't get this to work.
Thanks,
Ben