gpt4 book ai didi

r - 在 GTSummary 中对行进行分组

转载 作者:行者123 更新时间:2023-12-04 17:21:54 31 4
gpt4 key购买 nike

我正在尝试对一些行/变量(分类的和连续的)进行分组,以帮助提高大型数据集中的表格可读性。

这是虚拟数据集:

library(gtsummary)
library(tidyverse)
library(gt)
set.seed(11012021)

# Create Dataset
PIR <-
tibble(
siteidn = sample(c("1324", "1329", "1333", "1334"), 5000, replace = TRUE, prob = c(0.2, 0.45, 0.15, 0.2)) %>% factor(),
countryname = sample(c("NZ", "Australia"), 5000, replace = TRUE, prob = c(0.3, 0.7)) %>% factor(),
hospt = sample(c("Metropolitan", "Rural"), 5000, replace = TRUE, prob = c(0.65, 0.35)) %>% factor(),
age = rnorm(5000, mean = 60, sd = 20),
apache2 = rnorm(5000, mean = 18.5, sd=10),
apache3 = rnorm(5000, mean = 55, sd=20),
mechvent = sample(c("Yes", "No"), 5000, replace = TRUE, prob = c(0.4, 0.6)) %>% factor(),
sex = sample(c("Female", "Male"), 5000, replace = TRUE) %>% factor(),
patient = TRUE
) %>%
mutate(patient_id = row_number())%>%
group_by(
siteidn) %>% mutate(
count_site = row_number() == 1L) %>%
ungroup()%>%
group_by(
patient_id) %>% mutate(
count_pt = row_number() == 1L) %>%
ungroup()

然后我使用以下代码生成我的表:

t1 <- PIR %>% 
select(patientn = count_pt, siten = count_site, age, sex, apache2, apache3, apache2, mechvent, countryname) %>%
tbl_summary(
by = countryname,
missing = "no",
statistic = list(
patientn ~ "{n}",
siten ~ "{n}",
age ~ "{mean} ({sd})",
apache2 ~ "{mean} ({sd})",
mechvent ~ "{n} ({p}%)",
sex ~ "{n} ({p}%)",
apache3 ~ "{mean} ({sd})"),
label = list(
siten = "Number of ICUs",
patientn = "Number of Patients",
age = "Age",
apache2 = "APACHE II Score",
mechvent = "Mechanical Ventilation",
sex = "Sex",
apache3 = "APACHE III Score")) %>%
modify_header(stat_by = "**{level}**") %>%
add_overall(col_label = "**Overall**")

t2 <- PIR %>%
select(patientn = count_pt, siten = count_site, age, sex, apache2, apache3, apache2, mechvent, hospt) %>%
tbl_summary(
by = hospt,
missing = "no",
statistic = list(
patientn ~ "{n}",
siten ~ "{n}",
age ~ "{mean} ({sd})",
apache2 ~ "{mean} ({sd})",
mechvent ~ "{n} ({p}%)",
sex ~ "{n} ({p}%)",
apache3 ~ "{mean} ({sd})"),
label = list(
siten = "Number of ICUs",
patientn = "Number of Patients",
age = "Age",
apache2 = "APACHE II Score",
mechvent = "Mechanical Ventilation",
sex = "Sex",
apache3 = "APACHE III Score")) %>%
modify_header(stat_by = "**{level}**")

tbl <-
tbl_merge(
tbls = list(t1, t2),
tab_spanner = c("**Country**", "**Hospital Type**")
) %>%
modify_spanning_header(stat_0_1 ~ NA) %>%
modify_footnote(everything() ~ NA)

这会产生下表:

Table 1

我想将某些行组合在一起以便于阅读。理想情况下,我希望表格看起来像这样:

Table 1 Ideal

我尝试使用 gt 包,代码如下:

tbl <-
tbl_merge(
tbls = list(t1, t2),
tab_spanner = c("**Country**", "**Hospital Type**")
) %>%
modify_spanning_header(stat_0_1 ~ NA) %>%
modify_footnote(everything() ~ NA) %>%
as_gt() %>%
gt::tab_row_group(
group = "Severity of Illness Scores",
rows = 7:8) %>%
gt::tab_row_group(
group = "Patient Demographics",
rows = 3:6) %>%
gt::tab_row_group(
group = "Numbers",
rows = 1:2)

这会生成所需的表:

Table 1 Sections

我在执行此操作时遇到了一些问题。

  1. 当我尝试使用行名称(变量)时,会出现一条错误消息(无法子集化不存在的列...)。有没有办法通过使用变量名来做到这一点?对于较大的表,我在使用分配行名称的行号方法时遇到了一些麻烦。当单个变量在移动到末尾以说明分组行时丢失了它的位置时尤其如此。

  2. 有没有办法在管道进入 tbl_summary 之前执行此操作?虽然我喜欢这个表格的输出,但我使用 Word 作为统计报告的输出文档,并且希望能够在需要时(或由我的合作者)在 Word 中格式化表格。我通常使用 gtsummary::as_flextable 进行表输出。

再次感谢,

最佳答案

  1. When I try to use the row names (variables), an error message comes up (Can't subset columns that don't exist...). Is there a way to do this by using the variable names? With larger tables, I am getting into some trouble with using the row numbers method of assigning row names. This is particularly true when there is a single variable that loses its place as it's moved to the end to account for the grouped rows.

有两种方法可以解决这个问题,1. 为每个组构建单独的表,然后将它们堆叠起来,以及 2. 添加一个分组列到 .$table_body 然后按新的分组 tibble变量。

library(gtsummary)
library(dplyr)
packageVersion("gtsummary")
#> '1.3.6'

# Method 1 - Stack separate tables
t1 <- trial %>% select(age) %>% tbl_summary()
t2 <- trial %>% select(grade) %>% tbl_summary()

tbl1 <-
tbl_stack(
list(t1, t2),
group_header = c("Demographics", "Tumor Characteristics")
) %>%
modify_footnote(all_stat_cols() ~ NA)

# Method 2 - build a grouping variable
tbl2 <-
trial %>%
select(age, grade) %>%
tbl_summary() %>%
modify_table_body(
mutate,
groupname_col = case_when(variable == "age" ~ "Deomgraphics",
variable == "grade" ~ "Tumor Characteristics")
)

enter image description here

2.Is there a way to do this prior to piping into tbl_summary? Although I like the output of this table, I use Word as my output document for statistical reports and would like the ability to be able to format the tables in Word if need be (or by my collaborators). I usually use gtsummary::as_flextable for table output.

上面的示例在导出为 gt 格式之前修改了表格,因此您可以将这些示例导出为 flextable。然而,flextable 没有相同的内置标题行功能(或者至少我不知道它,并且不要在 as_flex_table() 中使用它),并且输出看起来像下表。我建议从 GitHub 安装 gt 的开发版本并导出为 RTF(受 Word 支持)——他们在过去几个月对 RTF 输出做了很多更新,它可能适合你。

enter image description here

关于r - 在 GTSummary 中对行进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65665465/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com