0

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
user438383
  • 5,716
  • 8
  • 28
  • 43
  • 1
    I tried to edit your table but I couldn't make it into a valid table. Can you please post a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your data with `dput(head(doctors_region))`? The code looks OK if the data is in the correct format so I suspect it isn't... – SamR Jul 21 '22 at 17:46

1 Answers1

0

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.

mhovd
  • 3,724
  • 2
  • 21
  • 47