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

后端 未结 2 493
走了就别回头了
走了就别回头了 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:34

    By using cut and dplyr

    dat$Class=cut(dat$Population,c(0,50000,100000,Inf),labels=c('0-50000','50000-100000','>100000'))
    library(dplyr)
    d1=dat%>%group_by(Class,Region)%>%summarise(count=n())
    d2=dat%>%group_by(Class)%>%summarise(count=n(),Region='All')
    bind_rows(d1,d2)
    
              Class   Region count
                  
     1      0-50000 Nordeste     9
     2      0-50000    Norte     8
     3      0-50000      Sul     6
     4 50000-100000 Nordeste     1
     5 50000-100000    Norte     1
     6 50000-100000      Sul     2
     7      >100000    Norte     1
     8      >100000      Sul     2
     9      0-50000      All    23
    10 50000-100000      All     4
    11      >100000      All     3
    

提交回复
热议问题