Trying to count total cases by city and getting an error in my coding

Hello,

I’m trying to count total cases by city. My grouping code worked but my summarise code did not. Can anyone see my error(s)? Reprex below.

Thank you, Alicia

pacman::p_load(
rio, # for importing data
here, # for locating files
skimr, # for reviewing the data
janitor, # for data cleaning
epikit, # creating age categories
gtsummary, # creating tables
apyramid, # for age/sex pyramids
RColorBrewer, # for colour palettes
viridis, # for color blind friendly color scales
scales, # percents in tables
flextable, # for making pretty tables
gghighlight, # highlighting plot parts
ggExtra, # special plotting functions
tsibble, # for epiweeks and other time series analyses
lubridate, # for dates
datapasta,
reprex,
tidyverse # for data management and visualization
)

demo ← data.frame(
stringsAsFactors = FALSE,
pid = c(β€œ9174d755abce7fb7”,
β€œ469fd41756126126”,β€œ9fb6d7c3a1be6987”,β€œ6b2c6f9d5980edb8”,
β€œ4c3ebec8e4794bee”,β€œ4c3ebec8e4794bee”,β€œ53cce1871a2312a3”,
β€œ2b6134374e2562cf”,β€œ2b6134374e2562cf”,β€œdc1f54eccf6a0588”),
city = c(β€œAtlanta”,β€œUnion City”,
β€œAtlanta”,β€œAtlanta”,β€œAtlanta”,β€œEast Point”,β€œAlpharetta”,
β€œAtlanta”,β€œEast Point”,β€œAlpharetta”),
gender = c(β€œMale”,β€œFemale”,β€œFemale”,
β€œMale”,β€œFemale”,β€œFemale”,β€œMale”,β€œFemale”,β€œFemale”,
β€œFemale”)
)

try to get total cases by city

demo %>%
group_by(city) %>%
summarise(total_cases = count(pid))

#> Error in summarise():
#> :information_source: In argument: total_cases = count(pid).
#> :information_source: In group 1: city = "Alpharetta".
#> Caused by error in UseMethod():
#> ! no applicable method for β€˜count’ applied to an object of class β€œcharacter”


<sup>Created on 2024-03-06 with [reprex v2.1.0](https://reprex.tidyverse.org)</sup>

<details style="margin-bottom:10px;">
<summary>
Session info
</summary>

``` r
sessionInfo()
#> R version 4.3.2 (2023-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19043)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.utf8 
#> [2] LC_CTYPE=English_United States.utf8   
#> [3] LC_MONETARY=English_United States.utf8
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.utf8    
#> 
#> time zone: America/Denver
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] forcats_1.0.0      stringr_1.5.1      dplyr_1.1.4        purrr_1.0.2       
#>  [5] readr_2.1.5        tidyr_1.3.1        tibble_3.2.1       tidyverse_2.0.0   
#>  [9] reprex_2.1.0       datapasta_3.1.0    lubridate_1.9.3    tsibble_1.1.4     
#> [13] ggExtra_0.10.1     gghighlight_0.4.1  ggplot2_3.4.4      flextable_0.9.4   
#> [17] scales_1.3.0       viridis_0.6.5      viridisLite_0.4.2  RColorBrewer_1.1-3
#> [21] apyramid_0.1.3     gtsummary_1.7.2    epikit_0.1.6       janitor_2.2.0     
#> [25] skimr_2.1.5        here_1.0.1         rio_1.0.1         
#> 
#> loaded via a namespace (and not attached):
#>  [1] DBI_1.2.1               gridExtra_2.3           rlang_1.1.3            
#>  [4] magrittr_2.0.3          snakecase_0.11.1        e1071_1.7-14           
#>  [7] compiler_4.3.2          systemfonts_1.0.5       vctrs_0.6.5            
#> [10] httpcode_0.3.0          pkgconfig_2.0.3         crayon_1.5.2           
#> [13] fastmap_1.1.1           ellipsis_0.3.2          utf8_1.2.4             
#> [16] promises_1.2.1          rmarkdown_2.25          tzdb_0.4.0             
#> [19] anytime_0.3.9           ragg_1.2.7              xfun_0.41              
#> [22] jsonlite_1.8.8          later_1.3.2             uuid_1.2-0             
#> [25] R6_2.5.1                stringi_1.8.3           Rcpp_1.0.12            
#> [28] knitr_1.45              base64enc_0.1-3         pacman_0.5.1           
#> [31] httpuv_1.6.14           timechange_0.3.0        tidyselect_1.2.0       
#> [34] rstudioapi_0.15.0       yaml_2.3.8              miniUI_0.1.1.1         
#> [37] curl_5.2.0              shiny_1.8.0             withr_3.0.0            
#> [40] askpass_1.2.0           evaluate_0.23           sf_1.0-15              
#> [43] units_0.8-5             proxy_0.4-27            zip_2.3.1              
#> [46] xml2_1.3.6              pillar_1.9.0            KernSmooth_2.23-22     
#> [49] generics_0.1.3          rprojroot_2.0.4         hms_1.1.3              
#> [52] munsell_0.5.0           xtable_1.8-4            class_7.3-22           
#> [55] glue_1.7.0              gdtools_0.3.5           tools_4.3.2            
#> [58] gfonts_0.2.0            data.table_1.15.0       fs_1.6.3               
#> [61] grid_4.3.2              colorspace_2.1-0        repr_1.1.6             
#> [64] cli_3.6.2               textshaping_0.3.7       officer_0.6.3          
#> [67] fontBitstreamVera_0.1.1 fansi_1.0.6             broom.helpers_1.14.0   
#> [70] gt_0.10.1               gtable_0.3.4            digest_0.6.34          
#> [73] fontquiver_0.2.1        classInt_0.4-10         crul_1.4.0             
#> [76] htmltools_0.5.7         lifecycle_1.0.4         mime_0.12              
#> [79] fontLiberation_0.1.0    openssl_2.1.1
1 Like

Hello,

The count() function is meant to be used on it’s own, not within the summarize() function, please see below for two ways to achieve counting cases by city:

# loading packages
library(tidyverse)

# creating fake data
demo <- data.frame(
    stringsAsFactors = FALSE,
    pid = c("9174d755abce7fb7",
                     "469fd41756126126","9fb6d7c3a1be6987","6b2c6f9d5980edb8",
                     "4c3ebec8e4794bee","4c3ebec8e4794bee","53cce1871a2312a3",
                     "2b6134374e2562cf","2b6134374e2562cf","dc1f54eccf6a0588"),
    city = c("Atlanta","Union City",
                        "Atlanta","Atlanta","Atlanta","East Point","Alpharetta",
                        "Atlanta","East Point","Alpharetta"),
    gender = c("Male","Female","Female",
                            "Male","Female","Female","Male","Female","Female",
                            "Female")
)

# counting by city
demo |>
    count(city)
#>         city n
#> 1 Alpharetta 2
#> 2    Atlanta 5
#> 3 East Point 2
#> 4 Union City 1

demo |>
    group_by(city) |>
    summarize(n = n())
#> # A tibble: 4 Γ— 2
#>   city           n
#>   <chr>      <int>
#> 1 Alpharetta     2
#> 2 Atlanta        5
#> 3 East Point     2
#> 4 Union City     1

Created on 2024-03-06 with reprex v2.1.0

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.1 (2023-06-16)
#>  os       macOS Ventura 13.6.3
#>  system   x86_64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Toronto
#>  date     2024-03-06
#>  pandoc   3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.2   2023-12-11 [1] CRAN (R 4.3.0)
#>  colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.3.0)
#>  digest        0.6.34  2024-01-11 [1] RSPM (R 4.3.0)
#>  dplyr       * 1.1.4   2023-11-17 [1] CRAN (R 4.3.0)
#>  evaluate      0.23    2023-11-01 [1] CRAN (R 4.3.0)
#>  fansi         1.0.6   2023-12-08 [1] CRAN (R 4.3.0)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.3.0)
#>  forcats     * 1.0.0   2023-01-29 [1] CRAN (R 4.3.0)
#>  fs            1.6.3   2023-07-20 [1] CRAN (R 4.3.0)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.3.0)
#>  ggplot2     * 3.5.0   2024-02-23 [1] RSPM (R 4.3.0)
#>  glue          1.7.0   2024-01-09 [1] RSPM (R 4.3.0)
#>  gtable        0.3.4   2023-08-21 [1] CRAN (R 4.3.0)
#>  hms           1.1.3   2023-03-21 [1] CRAN (R 4.3.0)
#>  htmltools     0.5.7   2023-11-03 [1] CRAN (R 4.3.0)
#>  knitr         1.45    2023-10-30 [1] CRAN (R 4.3.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.3.0)
#>  lubridate   * 1.9.3   2023-09-27 [1] CRAN (R 4.3.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.3.0)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.3.0)
#>  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.3.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.3.0)
#>  purrr       * 1.0.2   2023-08-10 [1] CRAN (R 4.3.0)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.3.0)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.3.0)
#>  R.oo          1.26.0  2024-01-24 [1] RSPM (R 4.3.0)
#>  R.utils       2.12.3  2023-11-18 [1] CRAN (R 4.3.0)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.3.0)
#>  readr       * 2.1.5   2024-01-10 [1] RSPM (R 4.3.0)
#>  reprex        2.1.0   2024-01-11 [1] RSPM (R 4.3.0)
#>  rlang         1.1.3   2024-01-10 [1] RSPM (R 4.3.0)
#>  rmarkdown     2.25    2023-09-18 [1] CRAN (R 4.3.0)
#>  rstudioapi    0.15.0  2023-07-07 [1] CRAN (R 4.3.0)
#>  scales        1.3.0   2023-11-28 [1] CRAN (R 4.3.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.3.0)
#>  stringi       1.8.3   2023-12-11 [1] CRAN (R 4.3.0)
#>  stringr     * 1.5.1   2023-11-14 [1] CRAN (R 4.3.0)
#>  styler        1.10.2  2023-08-29 [1] CRAN (R 4.3.0)
#>  tibble      * 3.2.1   2023-03-20 [1] CRAN (R 4.3.0)
#>  tidyr       * 1.3.1   2024-01-24 [1] RSPM (R 4.3.0)
#>  tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.3.0)
#>  tidyverse   * 2.0.0   2023-02-22 [1] CRAN (R 4.3.0)
#>  timechange    0.3.0   2024-01-18 [1] RSPM (R 4.3.0)
#>  tzdb          0.4.0   2023-05-12 [1] CRAN (R 4.3.0)
#>  utf8          1.2.4   2023-10-22 [1] CRAN (R 4.3.0)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.3.0)
#>  withr         3.0.0   2024-01-16 [1] RSPM (R 4.3.0)
#>  xfun          0.42    2024-02-08 [1] RSPM (R 4.3.0)
#>  yaml          2.3.8   2023-12-11 [1] CRAN (R 4.3.0)
#> 
#>  [1] /Users/timothychisamore/Library/R/x86_64/4.3/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

All the best,

Tim