I agree with Ben, always good to include a dataset we can run on our machine, and an example of what you would like the output look like. Below is a code example that adresses most of your questions.
- Is there a way to get a total row for a factor variable that is not the patient number?
I am not sure what you're looking for here. More details please.
- Is it possible to have an overall column inserted after merging the tables (so that the overall column does not come under the Country heading)?
Yes, you can use the modify_spanning_header()
function to remove the header above the Overall column.
- Is there a way to create a row for the number of patients and not have those details in the headings?
Yes, if you create a new column in your dataset that is TRUE for all observations, we can summarize that column and report the N.
Also, if you're only doing cross tabulations of a single variable, you should look into the tbl_cross()
function. It adds the total rows automatically.
library(gtsummary)
library(tidyverse)
set.seed(20210108)
# create dummy dataset
PIR <-
tibble(
siteidn = sample(c("1325", "1324", "1329"), 100, replace = TRUE) %>% factor(),
countryname = sample(c("NZ", "Australia"), 100, replace = TRUE) %>% factor(),
hospt = sample(c("Metro", "Rural"), 100, replace = TRUE) %>% factor(),
patient = TRUE
) %>%
group_by(siteidn) %>%
mutate(
count_site = row_number() == 1L # one TRUE per site
) %>%
ungroup() %>%
labelled::set_variable_labels(siteidn = "Number of ICUs", # Assigning labels
patient = "N")
t1 <- PIR %>%
select(patient, siteidn, countryname) %>%
tbl_summary(
by = countryname,
missing = "no",
statistic = patient ~ "{n}" # only print N for the top row
) %>%
modify_header(stat_by = "**{level}**") %>% # Remove the Ns from the header row
add_overall(col_label = "**Overall**")
t2 <- PIR %>%
select(patient, siteidn, hospt) %>%
tbl_summary(
by = hospt,
missing = "no",
statistic = patient ~ "{n}" # only print N for the top row
) %>%
modify_header(stat_by = "**{level}**") # Remove the Ns from the header row
tbl <-
tbl_merge(
tbls = list(t1, t2),
tab_spanner = c("**Country**", "**Hospital Type**")
) %>%
bold_labels() %>%
italicize_levels() %>%
# remove spanning header for overall column, use `show_header_names(tbl)` to print column names
modify_spanning_header(stat_0_1 ~ NA) %>%
modify_footnote(everything() ~ NA) # remove footnote, as it's not informative in this setting
EDIT: After clarification from original poster, adding another example of how one could present the Ns.
The table below shows two ways to show the Ns for the patients and the number of sites. The first is on two lines with two variables, and the last line is a way the information can be presented on a single line.
t1 <- PIR %>%
select(patient, site_only = count_site, combination = count_site, countryname) %>%
tbl_summary(
by = countryname,
missing = "no",
statistic = list(c(patient, site_only) ~ "{n}",
combination ~ "Site N {n}; Total N {N}")
)