Home > OS >  Group by and count with R
Group by and count with R

Time:07-22

I have a table in R that kinda looks like this:

| DoctorID | Region | | AHDCBA | 4 15 | | ABHAHF | 1 8 T4 | . . . . and so on. Both columns are character types. I want to know how many doctors are there in each region. I tried this code but it's giving me errors. If anyone could help me i'd really appreciate it.

doctors_region <- doctors %>%
 group_by(Region, DoctorID)%>%
 summarise(number = n())

doctors_region

CodePudding user response:

Try using count() instead, e.g.

doctors_region <- doctors %>%
 group_by(Region, DoctorID)%>%
 count()

I don't have your data, so here is an example using mtcars

library(dplyr)

mtcars %>% 
  group_by(am, gear) %>% 
  count()
#> # A tibble: 4 x 3
#> # Groups:   am, gear [4]
#>      am  gear     n
#>   <dbl> <dbl> <int>
#> 1     0     3    15
#> 2     0     4     4
#> 3     1     4     8
#> 4     1     5     5

Created on 2022-07-21 by the reprex package (v2.0.1)

Here is the documentation, which also has more examples.

  •  Tags:  
  • r
  • Related