Error when creating descriptive table (Reprex practice)

Hello,

I am working on the Influenza outbreak dataset. I am creating a table with detailed statistics and received an error. I tried looking in the Epi R Handbook but could not figure it out. Any help will be greatly appreciated. See the Reprex below: Thanks in advance.

#H7N9 Influenza Outbreak
# install and load packages
pacman::p_load(rio, janitor, scales, tidyverse, datapasta, reprex)

H7N9_influenza_demo <- data.frame(
  stringsAsFactors = FALSE,
  check.names = FALSE,
  case_id = c(1, 2, 4, 5, 6),
  province = c("Shanghai", "Shanghai", "Jiangsu","Jiangsu","Jiangsu", "Jiangsu", "Zhejiang", "Zhejiang", "Shanghai", "Zhejiang"),
  result = c("Death", "Death", NA, "Recovered", "Death", "Death","Death", NA, "Death","Death"),
  date_of_symptoms = c("2013-02-19","2013-02-27", "2013-03-19","2013-03-19","2013-03-21", "2013-03-20", "2013-03-07", "2013-03-25","2013-03-28", "2013-03-29")
)

province_info <- H7N9_influenza_demo %>% 
  group_by(province) %>%                    
  summarise(
    n_cases   = n(),                                #the number of cases for each province
    n_death = n(result == "Death", na.rm = TRUE),   #the number of deaths in each province
    max_sym = max(date_of_symptoms, na.rm = T),  #the latest date of symptom onset in each province
    pct_death = percent(n_death / n_cases))  #the percent of cases who died in each province
#> Error in `summarise()`:
#> โ„น In argument: `n_death = n(result == "Death", na.rm = TRUE)`.
#> โ„น In group 1: `province = "Jiangsu"`.
#> Caused by error in `n()`:
#> ! unused arguments (result == "Death", na.rm = TRUE)

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

Session info
sessioninfo::session_info()
#> โ”€ Session info โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
#>  setting  value
#>  version  R version 4.3.2 (2023-10-31 ucrt)
#>  os       Windows 10 x64 (build 19044)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United States.utf8
#>  ctype    English_United States.utf8
#>  tz       America/Los_Angeles
#>  date     2024-03-06
#>  pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> โ”€ Packages โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
#>  package     * version date (UTC) lib source
#>  cli           3.6.2   2023-12-11 [1] CRAN (R 4.3.2)
#>  colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.3.2)
#>  datapasta   * 3.1.0   2020-01-17 [1] CRAN (R 4.3.3)
#>  digest        0.6.34  2024-01-11 [1] CRAN (R 4.3.2)
#>  dplyr       * 1.1.4   2023-11-17 [1] CRAN (R 4.3.2)
#>  evaluate      0.23    2023-11-01 [1] CRAN (R 4.3.2)
#>  fansi         1.0.6   2023-12-08 [1] CRAN (R 4.3.2)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.3.2)
#>  forcats     * 1.0.0   2023-01-29 [1] CRAN (R 4.3.2)
#>  fs            1.6.3   2023-07-20 [1] CRAN (R 4.3.2)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.3.2)
#>  ggplot2     * 3.4.4   2023-10-12 [1] CRAN (R 4.3.2)
#>  glue          1.7.0   2024-01-09 [1] CRAN (R 4.3.2)
#>  gtable        0.3.4   2023-08-21 [1] CRAN (R 4.3.2)
#>  hms           1.1.3   2023-03-21 [1] CRAN (R 4.3.2)
#>  htmltools     0.5.7   2023-11-03 [1] CRAN (R 4.3.2)
#>  janitor     * 2.2.0   2023-02-02 [1] CRAN (R 4.3.2)
#>  knitr         1.45    2023-10-30 [1] CRAN (R 4.3.2)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.3.2)
#>  lubridate   * 1.9.3   2023-09-27 [1] CRAN (R 4.3.2)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.3.2)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.3.2)
#>  pacman        0.5.1   2019-03-11 [1] CRAN (R 4.3.2)
#>  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.3.2)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.3.2)
#>  purrr       * 1.0.2   2023-08-10 [1] CRAN (R 4.3.2)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.3.2)
#>  readr       * 2.1.5   2024-01-10 [1] CRAN (R 4.3.2)
#>  reprex      * 2.1.0   2024-01-11 [1] CRAN (R 4.3.2)
#>  rio         * 1.0.1   2023-09-19 [1] CRAN (R 4.3.2)
#>  rlang         1.1.3   2024-01-10 [1] CRAN (R 4.3.2)
#>  rmarkdown     2.25    2023-09-18 [1] CRAN (R 4.3.2)
#>  rstudioapi    0.15.0  2023-07-07 [1] CRAN (R 4.3.2)
#>  scales      * 1.3.0   2023-11-28 [1] CRAN (R 4.3.2)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.3.2)
#>  snakecase     0.11.1  2023-08-27 [1] CRAN (R 4.3.2)
#>  stringi       1.8.3   2023-12-11 [1] CRAN (R 4.3.2)
#>  stringr     * 1.5.1   2023-11-14 [1] CRAN (R 4.3.2)
#>  tibble      * 3.2.1   2023-03-20 [1] CRAN (R 4.3.2)
#>  tidyr       * 1.3.1   2024-01-24 [1] CRAN (R 4.3.2)
#>  tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.3.2)
#>  tidyverse   * 2.0.0   2023-02-22 [1] CRAN (R 4.3.2)
#>  timechange    0.3.0   2024-01-18 [1] CRAN (R 4.3.2)
#>  tzdb          0.4.0   2023-05-12 [1] CRAN (R 4.3.2)
#>  utf8          1.2.4   2023-10-22 [1] CRAN (R 4.3.2)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.3.2)
#>  withr         3.0.0   2024-01-16 [1] CRAN (R 4.3.2)
#>  xfun          0.41    2023-11-01 [1] CRAN (R 4.3.2)
#>  yaml          2.3.8   2023-12-11 [1] CRAN (R 4.3.2)
#> 
#>  [1] C:/Users/OR0285464/AppData/Local/R/win-library/4.3
#>  [2] C:/Program Files/R/R-4.3.2/library
#> 
#> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
1 Like

Hi Laxmi

There is a minor error on calculating n_death, please use sum() function instead of n function. It should look like this:

province_info <- H7N9_influenza_demo %>% 
  group_by(province) %>%                    
  summarise(
    n_cases   = n(),                                #the number of cases for each province
    n_death = sum(result == "Death", na.rm = TRUE),   #the number of deaths in each province
    max_sym = max(date_of_symptoms, na.rm = T),  #the latest date of symptom onset in each province
    pct_death = percent(n_death / n_cases)) 
1 Like