如何使用 tbl_summary 获取唯一观察值的数量

How to get number of unique observations using tbl_summary

在下面的数据中,我有 7 名患者因 9 种情况接受了手术,其中两名患者因两种不同的原因在同一天接受了手术。当我尝试使用 tbl_summary 函数总结此数据时,它显示观察总数为 9。我想添加患者总数,即 n = 7.I 我不明白我该如何添加table 中的信息。你们能帮我解决这个问题吗?

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 (%)")

好的!如果您为第一个参与者索引添加一个指标并将该新变量包含在摘要中 table,您可以将其包含在摘要中。下面的示例!

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 (%)")