I have a data frame that looks like this:
dat <- structure(list(Geocode = c(\"1100015\", \"1100023\", \"1100031\", \"1100049\",
\"1100056\", \"1100064\", \"1
Here is a quick and dirty method, might update this later to make it more clean and avoid having to bind_rows()
Try the following:
library(tidyverse)
dat_1 <- dat %>%
mutate(population_breaks = case_when(Population <= 50000 ~ "0-50000",
Population >= 50000 & Population <= 100000 ~ "50000-100000",
Population >= 100000 ~ ">100000")) %>%
group_by(population_breaks) %>%
count(Region)
dat_2 <- dat %>%
mutate(population_breaks = case_when(Population <= 50000 ~ "0-50000",
Population >= 50000 & Population <= 100000 ~ "50000-100000",
Population >= 100000 ~ ">100000")) %>%
group_by(population_breaks) %>%
count(population_breaks) %>%
mutate(Region = "All")
bind_rows(dat_1, dat_2)
Which returns:
# A tibble: 11 x 3
# Groups: population_breaks [3]
population_breaks Region n
1 0-50000 Nordeste 9
2 50000-100000 Nordeste 1
3 >100000 Norte 1
4 0-50000 Norte 8
5 50000-100000 Norte 1
6 >100000 Sul 2
7 0-50000 Sul 6
8 50000-100000 Sul 2
9 >100000 All 3
10 0-50000 All 23
11 50000-100000 All 4