r - 具有分类变量总计的 R gsummary 行
问题描述
我有一个包含大约 700,000 名患者的数据集,其中我有医院站点 ID(因子变量)。我想创建一个可见医院数量的行(这与患者数量是分开的)。除了一个整体列之外,我还有 3 个分类变量作为我的列。
目前,每个医院 ID 都有一个单独的行,其中包含每个类别中每个站点的患者数量。
我的代码如下:
t1 <- PIR %>%
select(siteidn, countryname) %>%
tbl_summary(by = countryname ,missing = "no",
label = list(
siteidn = "Number of ICUs"),
statistic = list(
all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n} ({p}%)")) %>%
bold_labels() %>%
italicize_levels() %>%
add_overall()
t2 <- PIR %>%
select(siteidn, hospt) %>%
tbl_summary(by = hospt ,missing = "no",
label = list(
siteidn = "Number of ICUs"),
statistic = list(
all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n} ({p}%)")) %>%
bold_labels() %>%
italicize_levels()
t3 <- PIR %>%
select(siteidn, iculevelname) %>%
tbl_summary(by = iculevelname ,missing = "no",
label = list(
siteidn = "Number of ICUs"),
statistic = list(
all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n} ({p}%)")) %>%
bold_labels() %>%
italicize_levels()
tbl_merge(
tbls = list(t1, t2, t3),
tab_spanner = c("**Country**", "**Hospital Type**", "**ICU Level**"))
这将产生下表:
可以看出,每个医院 ID 都有单独的一行。我想有一个单行,其中有每层医院的总数(即澳大利亚、新西兰、大都会等的医院总数)。
我的问题是:
- 有没有办法为不是患者编号的因素变量获得总行?
- 合并表格后是否可以插入一个整体列(以便整体列不在国家标题下)?
- 有没有办法为患者数量创建一行而不在标题中包含这些详细信息?
谢谢大家的时间。
本
补充:这是我希望桌子看起来像的图像。我为它的粗鲁道歉。我只想为 ICU 总数的因子变量设置一行,而不是在每个 ICU 中设置一行,其中包含患者人数(红色墨水)。
此外,有没有一种方法可以将 2 行分组到一个类似于因子变量(Green Ink)的共同标题下。
我很欣赏我的 R 技能是初级的。谢谢大家的耐心等待!
本
解决方案
我同意 Ben 的观点,包含一个我们可以在我们的机器上运行的数据集总是很好的,以及一个您希望输出看起来像的示例。下面是一个代码示例,可以解决您的大部分问题。
- 有没有办法为不是患者编号的因素变量获得总行?
我不确定你在这里寻找什么。请提供更多详细信息。
- 合并表格后是否可以插入一个整体列(以便整体列不在国家标题下)?
是的,您可以使用该modify_spanning_header()
功能删除“总体”列上方的标题。
- 有没有办法为患者数量创建一行而不在标题中包含这些详细信息?
是的,如果您在数据集中创建一个对所有观察结果都为 TRUE 的新列,我们可以总结该列并报告 N。
此外,如果您只是对单个变量进行交叉制表,则应该查看该tbl_cross()
函数。它会自动添加总行数。
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
编辑:在原始海报澄清后,添加另一个如何展示 Ns 的示例。
下表显示了两种显示患者 Ns 和站点数量的方法。第一行是有两个变量的两行,最后一行是信息可以在一行中呈现的一种方式。
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}")
)
推荐阅读
- ddev - Drupal 配置位置
- python - Python PULP 数组乘法约束
- powershell - 无法在输出文件中设置 PowerShell 结果
- java - 为什么 servlet 类中具有 firebase 的 childeventlistener 的自定义 java 方法无法完成?
- c++ - 使用 boost 从代码页 437 转换为 unicode
- oracle - 汇总每天的中断持续时间
- android - Flutter:更改 CircularProgressIndicator 颜色
- php - 我的会话变量不会通过不同的页面进行传递
- java - JBoss 7.00 没有在带有 Netbeans 8.2 的 macOS Sierra 中启动 ojdbc
- c# - 在 Hangfire 的 IdashboardAuthorizationFilter 中解码 access_token