I have been using R for a number of years but increasingly need to produce elegant tables for publications. My preference is to have R do this for me but was never able to devote the time to learn this and always ended up editing tables in Word.
I am currently trying to produce a pretty looking summary stats table of incident reports using the gtsummary package. My data is almost 150k rows and it contains the following (simplified):
Year <- c('2016','2017','2018','2016','2017','2018')
Type <- c('Fall','Concussion','Fall','Burn','Burn','Laceration')
Service <- c('Hospital','Hospital','Nursing home','Hospital','Trauma centre','Hospital')
Risk <- c(6,10,3,15,3,6)
dat <- cbind.data.frame(Year,Type,Service,Risk)
What I want to produce is a table using gtsummary where the rows are 'Type', the columns are grouped by Service and then by Year, and the data showing in the table cells is the mean Risk.
So far, I have been able to produce the table in the exact structure I want but I cannot figure out how to specify that the value in the cells should be the mean of Risk. Here's the code that will produce the above:
data %>%
select(c(Type,Service,Risk,Year)) %>%
tbl_strata(
strata = Service,
~ .x %>%
tbl_summary(by = Year, missing = "no")))
To be clear, I would like the table to have the following structure:
Would appreciate any pointers on this as i've tried everything!
You can get what you are after using the tbl_continuous()
function. Example below!
library(gtsummary)
#> #BlackLivesMatter
packageVersion("gtsummary")
#> [1] '1.7.1'
Year <- c('2016','2017','2018','2016','2017','2018')
Type <- c('Fall','Concussion','Fall','Burn','Burn','Laceration')
Service <- c('Hospital','Hospital','Nursing home','Hospital','Trauma centre','Hospital')
Risk <- c(6,10,3,15,3,6)
dat <-
cbind.data.frame(Year,Type,Service,Risk) |>
# filling in so each cell has at least one value
tidyr::complete(Year,Type,Service, fill = list(Risk = 1))
tbl <-
dat %>%
tbl_strata(
strata = Service,
~ .x %>%
tbl_continuous(
by = Year,
variable = Risk,
include = Type,
statistic = ~"{mean}"
) |>
modify_header(all_stat_cols() ~ "**{level}**")
)
Created on 2023-05-02 with reprex v2.0.2