Hi,
I can’t figure out how to get the percentages across the rows instead of down the columns in the table I’m producing with tbl_summary. This is for my own knowledge and is using fake data (i.e. not urgent).
I’ve tried googling and looking at various pages that come up. Looking at the information pages for tbl_summary and add_p, looking through the Epi R handbook - and I just can’t seem to figure it out. Below should reproduce the table I’m getting where percentages are calculated down the columns. I want the percentages calculated across the rows.
Thank you for your time and any assistance!
# load packages
pacman::p_load (
rio, #for importing data
here, #for file paths
janitor, #for data cleaning
epikit, #for creating age categories
flextable, #for formatting tables
scales, #for formatting values
gtsummary, #for pretty tables
ggExtra, #add additional plotting fxns to ggplot2 incl in tidyverse
gghighlight, #highlights portions of the plot
viridis, #for more color palettes, incl color-blind friendly palette
scales, #for formatting
lubridate, #for dates
apyramid, #for creating age pyramids
tsibble, #for epiweeks & other time series analyses
reprex,
datapasta,
tidyverse #for data management
)
# import data
linelist <- data.frame(
stringsAsFactors = FALSE,
race = c("Black","Black","White",
"White","Black","White","Black","White","White","Black",
"White",NA,"White","White","White",NA,NA,"Black",
"Black","Black","White","White","Black","White",
"White","Black","Asian","Black",NA,"White","White",
"Black",NA,"Black","White","White","Black","Black",
"Black","Black","Black","Black",NA,"Black","White",
"Black","Black","White","White","White","White",
"Black","White","White","Black","Other","Asian",
"White","White","White","White","Black","Black","Black",
"Asian","White","Black","White","White",NA,
"White","Black","Black","White","White","White","Asian",
"Black","White","Black","Black","White","White",
"Black","White","Black",NA,"Black","Black","Black",
"Black","Black","White","Other",NA,"White","Black",
"White",NA,"White"),
hospitalized = c("No","No","No","No","No",
"Yes","No","No",NA,NA,NA,NA,NA,NA,NA,NA,NA,
"No","No","Yes","No","Yes",NA,"Yes",NA,NA,"No",
"No",NA,NA,NA,NA,NA,"Yes","No","No",NA,"No","No",
NA,"No","No",NA,"No",NA,NA,NA,"Yes",NA,NA,
"No",NA,"No","No","No",NA,"No",NA,NA,"No",NA,NA,
NA,"No","No",NA,NA,"Yes","No",NA,NA,NA,"No",NA,
"No","No",NA,"Yes","No",NA,"No","No",NA,NA,NA,
"No",NA,"No",NA,"No","Yes",NA,NA,"No","No",
"No",NA,"No",NA,NA)
)
# table with gtsummary
linelist %>% select(
race, # values I want separated out by
hospitalized
) %>%
tbl_summary(by = hospitalized
) %>%
add_p() %>% # applies pearson's chi-square test
as_flex_table()
#> 50 observations missing `hospitalized` have been removed. To include these observations, use `forcats::fct_na_value_to_level()` on `hospitalized` column before passing to `tbl_summary()`.
Created on 2024-04-12 with reprex v2.1.0
Session info
sessionInfo()
#> R version 4.3.0 (2023-04-21 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19045)
#>
#> 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/New_York
#> 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.0 tibble_3.2.1 tidyverse_2.0.0
#> [9] datapasta_3.1.0 reprex_2.1.0 tsibble_1.1.3 apyramid_0.1.3
#> [13] lubridate_1.9.3 viridis_0.6.4 viridisLite_0.4.2 gghighlight_0.4.1
#> [17] ggplot2_3.4.4 ggExtra_0.10.1 gtsummary_1.7.2 scales_1.3.0
#> [21] flextable_0.9.4 epikit_0.1.5 janitor_2.2.0 here_1.0.1
#> [25] 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.0 png_0.1-8 systemfonts_1.0.5
#> [10] vctrs_0.6.5 httpcode_0.3.0 pkgconfig_2.0.3
#> [13] crayon_1.5.2 fastmap_1.1.1 backports_1.4.1
#> [16] ellipsis_0.3.2 utf8_1.2.4 promises_1.2.1
#> [19] rmarkdown_2.25 tzdb_0.4.0 anytime_0.3.9
#> [22] ragg_1.2.7 xfun_0.41 jsonlite_1.8.8
#> [25] highr_0.10 later_1.3.2 uuid_1.2-0
#> [28] broom_1.0.5 R6_2.5.1 stringi_1.8.3
#> [31] Rcpp_1.0.12 knitr_1.45 pacman_0.5.1
#> [34] httpuv_1.6.13 timechange_0.2.0 tidyselect_1.2.0
#> [37] rstudioapi_0.15.0 yaml_2.3.8 miniUI_0.1.1.1
#> [40] curl_5.2.0 shiny_1.8.0 withr_3.0.0
#> [43] askpass_1.2.0 evaluate_0.23 sf_1.0-15
#> [46] units_0.8-5 proxy_0.4-27 zip_2.3.0
#> [49] xml2_1.3.6 pillar_1.9.0 KernSmooth_2.23-20
#> [52] generics_0.1.3 rprojroot_2.0.4 hms_1.1.3
#> [55] munsell_0.5.0 xtable_1.8-4 class_7.3-21
#> [58] glue_1.7.0 gdtools_0.3.5 tools_4.3.0
#> [61] gfonts_0.2.0 data.table_1.14.10 fs_1.6.3
#> [64] grid_4.3.0 colorspace_2.1-0 cli_3.6.2
#> [67] textshaping_0.3.7 officer_0.6.3 fontBitstreamVera_0.1.1
#> [70] fansi_1.0.6 broom.helpers_1.14.0 gt_0.10.1
#> [73] gtable_0.3.4 digest_0.6.34 fontquiver_0.2.1
#> [76] classInt_0.4-10 crul_1.4.0 htmltools_0.5.7
#> [79] lifecycle_1.0.4 mime_0.12 fontLiberation_0.1.0
#> [82] openssl_2.1.1