I am working with census data in a region. I have the total, urban, and rural populations of each municipality in the region, identified by a municipality code. In my data frame, the three population types are "stacked" in one population
variable, such that rows containing municipality codes store total populations but not urban or rural ones:
location | population | mun_code |
---|---|---|
municipality_1 | 10000 | 1 |
urban | 6000 | 1 |
rural | 4000 | 1 |
municipality_2 | 15000 | 2 |
urban | 10000 | 2 |
rural | 5000 | 2 |
I want to construct a second data frame with one variable for each population type:
location | total_pop | urban_pop | rural_pop | mun_code |
---|---|---|---|---|
municipality_1 | 10000 | 6000 | 4000 | 1 |
municipality_2 | 15000 | 10000 | 5000 | 2 |
I have tried using pivot_wider()
from package tidyr, but can't seem to get the table to look the way I want.