In the data below, I have 7 patients who have undergone surgeries for 9 conditions, two of the patients have undergone surgery on the same day for two different reasons. When I try to summarise this data using tbl_summary function, it shows total number of observations as 9. I want to add total number of patients, which is n = 7.I am not understanding how do I add that information in the table. Could you guys please help me with this?
library(data.table)
library(tidyverse)
library(gtsummary)
participant.index = c(1,2,3,3,4,5,5,6,7)
repeat.instance = c(1,1,1,1,1,1,1,1,1)
indication.surgery = c("ibs","infection", "infection", "renalstones", "ibs", "infection",
"infection", "ibs","tumour")
date.surgery =c("2019-01-10", "2019-01-01", "2018-01-01", "2018-01-01", "2017-09-10",
"2000-09-09","2015-01-10","2015-01-10","2006-09-09")
mydata = data.table(participant.index,repeat.instance,indication.surgery,date.surgery)
mydata%>%
select(indication.surgery)%>%
tbl_summary
label = list(indication.surgery ~
"Indication for Surgery"),
statistic = list(all_categorical() ~ "{n} ({p}%)"),
missing_text = "(Missing Observations)",
percent = c("cell")
) %>%
modify_table_styling(spanning_header = "Indications for Surgery") %>%
modify_caption("**Table 1. Indication for Surgery**") %>%
modify_footnote(all_stat_cols() ~ "Number of observations, Frequency (%)")
Sure! If you add an indicator for the first participant index and include that new variable in the summary table, you can include it in the summary. Example Below!
library(data.table)
library(tidyverse)
library(gtsummary)
participant.index <- c(1, 2, 3, 3, 4, 5, 5, 6, 7)
repeat.instance <- c(1, 1, 1, 1, 1, 1, 1, 1, 1)
indication.surgery <- c(
"ibs", "infection", "infection", "renalstones", "ibs", "infection",
"infection", "ibs", "tumour"
)
date.surgery <- c(
"2019-01-10", "2019-01-01", "2018-01-01", "2018-01-01", "2017-09-10",
"2000-09-09", "2015-01-10", "2015-01-10", "2006-09-09"
)
mydata <-
data.table(participant.index, repeat.instance,
indication.surgery, date.surgery) %>%
group_by(participant.index) %>%
mutate(first.participant.index = row_number() == 1L,
.before = 1L) %>%
ungroup()
mydata %>%
select(first.participant.index, indication.surgery) %>%
tbl_summary(
label =
list(first.participant.index ~ "No. Unique Participants",
indication.surgery ~ "Indication for Surgery"),
statistic = list(all_categorical() ~ "{n} ({p}%)",
first.participant.index ~ "{n}"),
missing_text = "(Missing Observations)",
percent = c("cell")
) %>%
modify_table_styling(spanning_header = "Indications for Surgery") %>%
modify_caption("**Table 1. Indication for Surgery**") %>%
modify_footnote(all_stat_cols() ~ "Number of observations, Frequency (%)")