Sorting data frame rows

Note: This is a demonstration of a code challenge as part of the Intro to R course.

Hi Community,

I’ve retrieved public EMS calls into the ‘demo_data’ data frame below, and used the helpful forum on Applied Epi to create a frequency table by Call Type and Date. Now I’m a bit stuck, because I’m not sure what the most efficient and intuitive next step for learning which Call Type and Date (combined) had the highest frequency of EMS calls (assume the demo data represents a much, much larger dataset that will be harder to parse by visualization alone).
I’m wondering if you have advice? Is this an answer I can pull from a frequency table, or should I take a different approach to analyzing the data frame instead?

Thank you for the support!
Rachel

pacman::p_load(rio, lubridate, datapasta, reprex, tidyverse)

demo_data<- data.frame(
  stringsAsFactors = FALSE,
  CallNumber = c(241560871L,
                 241560939L,241562505L,
                 241570146L,241563077L,
                 241563031L,241560981L,
                 241563229L,241563178L,
                 241563511L),
  Date = c("2024-06-04",
           "2024-06-04","2024-06-04",
           "2024-06-05",
           "2024-06-04","2024-06-04",
           "2024-06-04","2024-06-04",
           "2024-06-04","2024-06-04"),
  Type = c("Medical Incident","Alarms",
           "Medical Incident",
           "Medical Incident","Medical Incident",
           "Medical Incident",
           "Citizen Assist / Service Call",
           "Medical Incident",
           "Medical Incident",
           "Medical Incident"))
  
#frequency by date
table <- demo_data %>% 
  group_by(Type, Date) %>% 
  summarise(
    total_calls  = n())
#> `summarise()` has grouped output by 'Type'. You can override using the
#> `.groups` argument.

#frequency by date and Call type
table<-demo_data %>% 
  group_by(Type, Date) %>% 
  summarise(count = n(), .groups = "drop") %>%
  pivot_wider(names_from = Type, values_from = count)

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

Session info
sessionInfo()
#> R version 4.3.3 (2024-02-29 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/Los_Angeles
#> 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    ggplot2_3.5.1  
#>  [9] tidyverse_2.0.0 reprex_2.1.0    datapasta_3.1.0 lubridate_1.9.3
#> [13] rio_1.0.1      
#> 
#> loaded via a namespace (and not attached):
#>  [1] gtable_0.3.5      compiler_4.3.3    tidyselect_1.2.1  scales_1.3.0     
#>  [5] yaml_2.3.8        fastmap_1.1.1     R6_2.5.1          generics_0.1.3   
#>  [9] knitr_1.46        munsell_0.5.1     tzdb_0.4.0        pillar_1.9.0     
#> [13] rlang_1.1.3       utf8_1.2.4        stringi_1.8.3     xfun_0.43        
#> [17] fs_1.6.4          timechange_0.3.0  cli_3.6.2         withr_3.0.0      
#> [21] magrittr_2.0.3    digest_0.6.35     grid_4.3.3        rstudioapi_0.16.0
#> [25] hms_1.1.3         lifecycle_1.0.4   vctrs_0.6.5       evaluate_0.23    
#> [29] glue_1.7.0        fansi_1.0.6       colorspace_2.1-0  pacman_0.5.1     
#> [33] rmarkdown_2.26    tools_4.3.3       pkgconfig_2.0.3   htmltools_0.5.8.1
1 Like

Hi Rachel,

If I am understanding your question correctly, I think what you want to do is to sort your data to yield the greatest counts:

# loading packages
library(tidyverse)

# creating fake data
demo_data <- data.frame(
    stringsAsFactors = FALSE,
    CallNumber = c(
        241560871L,
        241560939L,
        241562505L,
        241570146L,
        241563077L,
        241563031L,
        241560981L,
        241563229L,
        241563178L,
        241563511L
    ),
    Date = c(
        "2024-06-04",
        "2024-06-04",
        "2024-06-04",
        "2024-06-05",
        "2024-06-04",
        "2024-06-04",
        "2024-06-04",
        "2024-06-04",
        "2024-06-04",
        "2024-06-04"
    ),
    Type = c(
        "Medical Incident",
        "Alarms",
        "Medical Incident",
        "Medical Incident",
        "Medical Incident",
        "Medical Incident",
        "Citizen Assist / Service Call",
        "Medical Incident",
        "Medical Incident",
        "Medical Incident"
    )
) |>
    as_tibble()

# creating count data
count_data <- demo_data |>
    count(Type, Date)

# sorting count data
count_data |>
    arrange(desc(n))
#> # A tibble: 4 × 3
#>   Type                          Date           n
#>   <chr>                         <chr>      <int>
#> 1 Medical Incident              2024-06-04     7
#> 2 Alarms                        2024-06-04     1
#> 3 Citizen Assist / Service Call 2024-06-04     1
#> 4 Medical Incident              2024-06-05     1

Created on 2024-08-07 with reprex v2.1.1

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.1 (2024-06-14)
#>  os       macOS Sonoma 14.5
#>  system   x86_64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Toronto
#>  date     2024-08-07
#>  pandoc   3.1.11 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/x86_64/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.3   2024-06-21 [1] RSPM (R 4.4.0)
#>  colorspace    2.1-0   2023-01-23 [1] RSPM (R 4.4.0)
#>  digest        0.6.36  2024-06-23 [1] RSPM (R 4.4.0)
#>  dplyr       * 1.1.4   2023-11-17 [1] RSPM (R 4.4.0)
#>  evaluate      0.24.0  2024-06-10 [1] RSPM (R 4.4.0)
#>  fansi         1.0.6   2023-12-08 [1] RSPM (R 4.4.0)
#>  fastmap       1.2.0   2024-05-15 [1] RSPM (R 4.4.0)
#>  forcats     * 1.0.0   2023-01-29 [1] RSPM (R 4.4.0)
#>  fs            1.6.4   2024-04-25 [1] RSPM (R 4.4.0)
#>  generics      0.1.3   2022-07-05 [1] RSPM (R 4.4.0)
#>  ggplot2     * 3.5.1   2024-04-23 [1] RSPM (R 4.4.0)
#>  glue          1.7.0   2024-01-09 [1] RSPM (R 4.4.0)
#>  gtable        0.3.5   2024-04-22 [1] RSPM (R 4.4.0)
#>  hms           1.1.3   2023-03-21 [1] RSPM (R 4.4.0)
#>  htmltools     0.5.8.1 2024-04-04 [1] RSPM (R 4.4.0)
#>  knitr         1.48    2024-07-07 [1] RSPM (R 4.4.0)
#>  lifecycle     1.0.4   2023-11-07 [1] RSPM (R 4.4.0)
#>  lubridate   * 1.9.3   2023-09-27 [1] RSPM (R 4.4.0)
#>  magrittr      2.0.3   2022-03-30 [1] RSPM (R 4.4.0)
#>  munsell       0.5.1   2024-04-01 [1] RSPM (R 4.4.0)
#>  pillar        1.9.0   2023-03-22 [1] RSPM (R 4.4.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] RSPM (R 4.4.0)
#>  purrr       * 1.0.2   2023-08-10 [1] RSPM (R 4.4.0)
#>  R6            2.5.1   2021-08-19 [1] RSPM (R 4.4.0)
#>  readr       * 2.1.5   2024-01-10 [1] RSPM (R 4.4.0)
#>  reprex        2.1.1   2024-07-06 [1] RSPM (R 4.4.0)
#>  rlang         1.1.4   2024-06-04 [1] RSPM (R 4.4.0)
#>  rmarkdown     2.27    2024-05-17 [1] RSPM (R 4.4.0)
#>  rstudioapi    0.16.0  2024-03-24 [1] RSPM (R 4.4.0)
#>  scales        1.3.0   2023-11-28 [1] RSPM (R 4.4.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] RSPM (R 4.4.0)
#>  stringi       1.8.4   2024-05-06 [1] RSPM (R 4.4.0)
#>  stringr     * 1.5.1   2023-11-14 [1] RSPM (R 4.4.0)
#>  tibble      * 3.2.1   2023-03-20 [1] RSPM (R 4.4.0)
#>  tidyr       * 1.3.1   2024-01-24 [1] RSPM (R 4.4.0)
#>  tidyselect    1.2.1   2024-03-11 [1] RSPM (R 4.4.0)
#>  tidyverse   * 2.0.0   2023-02-22 [1] RSPM (R 4.4.0)
#>  timechange    0.3.0   2024-01-18 [1] RSPM (R 4.4.0)
#>  tzdb          0.4.0   2023-05-12 [1] RSPM (R 4.4.0)
#>  utf8          1.2.4   2023-10-22 [1] RSPM (R 4.4.0)
#>  vctrs         0.6.5   2023-12-01 [1] RSPM (R 4.4.0)
#>  withr         3.0.0   2024-01-16 [1] RSPM (R 4.4.0)
#>  xfun          0.45    2024-06-16 [1] RSPM (R 4.4.0)
#>  yaml          2.3.9   2024-07-05 [1] RSPM (R 4.4.0)
#> 
#>  [1] /Users/timothychisamore/Library/R/x86_64/4.4/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

All the best,

Tim