gtsummary 是一个基于 gt 包的数据汇总表生成包。
项目地址:https://github.com/ddsjoberg/gtsummary[1]
稳定版:
install.packages("gtsummary")
开发版:
remotes::install_github("ddsjoberg/gtsummary")
跟它的依赖包 gt 很类似。下面有动态图:
代码如下:
library(gtsummary)
# 造数据集
trial2 <- trial %>% dplyr::select(trt, age, grade, response)
# 汇总
table1 <- tbl_summary(trial2)
美观的表格就出来了!
再看一个例子,我们将数据分组汇总:
table2 <- tbl_summary(
trial2,
by = trt, # 分组
missing = "no" #隐藏缺失数据
) %>%
add_n() %>% # 增加观测值汇总
add_p() %>% # 增加组间比较的统计检验结果
bold_labels()
这样美观的表格有了,想要计算的显著性也有了。
来一个逻辑回归:
mod1 <- glm(response ~ trt + age + grade, trial, family = binomial)
t1 <- tbl_regression(mod1, exponentiate = TRUE)
增加一个生存分析 Cox 回归结果,并放到一起:
library(survival)
# build survival model table
t2 <-
coxph(Surv(ttdeath, death) ~ trt + grade + age, trial) %>%
tbl_regression(exponentiate = TRUE)
# merge tables
tbl_merge_ex1 <-
tbl_merge(
tbls = list(t1, t2),
tab_spanner = c("**Tumor Response**", "**Time to Death**")
)
简简单单几段代码解决了非常多的问题,上车吧。
更多的介绍和使用请看英文文档:http://www.danieldsjoberg.com/gtsummary/[2]
[1]https://github.com/ddsjoberg/gtsummary: https://github.com/ddsjoberg/gtsummary
[2]http://www.danieldsjoberg.com/gtsummary/: http://www.danieldsjoberg.com/gtsummary/