R - cut by breaks and count number of occurrences by group

后端 未结 2 476
走了就别回头了
走了就别回头了 2021-01-26 23:47

I have a data frame that looks like this:

dat <- structure(list(Geocode = c(\"1100015\", \"1100023\", \"1100031\", \"1100049\", 
\"1100056\", \"1100064\", \"1         


        
2条回答
  •  独厮守ぢ
    2021-01-27 00:14

    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
    

提交回复
热议问题