在 gtsummary 中添加频率和缺失值百分比
Add frequency and % of missing values in gtsummary
df_nhpi %>%
select(AGE, SEX, MAR_STAT, HEIGHT, WEIGHT, BMI, HTN, HTNMED, MI, Smoking, COPD, CANCER, DIABETES) %>%
tbl_summary(by = SEX,
label = list(MAR_STAT ~ 'Marital Status',
HTN ~ 'Hypertension',
HTNMED ~ 'Hypertension Medication',
MI ~ 'Heart Attack',
Smoking ~ 'Smoking Status',
COPD ~ 'Chronic Obstructive Pulmonary Disease'),
type = list(c("HTN","HTNMED", "MI", "COPD", "CANCER") ~ "categorical"),
missing = "ifany",
missing_text = "Unknown",
statistic = list(all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n} ({p}%)"),
digits = all_continuous() ~ 2, percent = "column") %>%
add_stat_label() %>%
add_p(test = all_continuous() ~ "t.test", pvalue_fun =
function(x) style_pvalue(x, digits = 3)) %>%
bold_p() %>%
modify_caption("**Table 1. Baseline Characteristics**") %>% bold_labels()
我正在尝试生成一个 table。但是,这里的问题是,我希望 % 用于跨列的缺失值(特别是对于分类变量),同时,我不希望在计算 p 值时包含缺失值。我正在尝试在单个代码块中执行此操作。有没有办法做到这一点,还是我应该采用传统方法?
过去三天我一直在搜索整个互联网。但是,我找不到任何适合我的情况。
PS:mutate 和 forcats 不起作用,因为它扭曲了我的 p 值。
我准备了两个解决方案,都报告了缺失数据的比例。希望其中之一适合您!
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.2'
# add % missing in new column
tbl1 <-
trial %>%
tbl_summary(
by = trt,
include = response,
type = all_dichotomous() ~ "categorical",
missing = "no"
) %>%
add_p() %>%
add_n(statistic = "{n_miss} ({p_miss}%)") %>%
modify_header(n = "**Missing**")
# prepare tbl_summary with rows for missing, then merge in p-values
tbl2 <-
trial %>%
dplyr::mutate(response = forcats::fct_explicit_na(factor(response))) %>%
tbl_summary(
by = trt,
include = response,
label = list(response = "Tumor Response")
) %>%
list(tbl1 %>% modify_column_hide(c(n, all_stat_cols()))) %>%
tbl_merge(tab_spanner = FALSE)
由 reprex package (v2.0.1)
创建于 2022-03-22
df_nhpi %>%
select(AGE, SEX, MAR_STAT, HEIGHT, WEIGHT, BMI, HTN, HTNMED, MI, Smoking, COPD, CANCER, DIABETES) %>%
tbl_summary(by = SEX,
label = list(MAR_STAT ~ 'Marital Status',
HTN ~ 'Hypertension',
HTNMED ~ 'Hypertension Medication',
MI ~ 'Heart Attack',
Smoking ~ 'Smoking Status',
COPD ~ 'Chronic Obstructive Pulmonary Disease'),
type = list(c("HTN","HTNMED", "MI", "COPD", "CANCER") ~ "categorical"),
missing = "ifany",
missing_text = "Unknown",
statistic = list(all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n} ({p}%)"),
digits = all_continuous() ~ 2, percent = "column") %>%
add_stat_label() %>%
add_p(test = all_continuous() ~ "t.test", pvalue_fun =
function(x) style_pvalue(x, digits = 3)) %>%
bold_p() %>%
modify_caption("**Table 1. Baseline Characteristics**") %>% bold_labels()
我正在尝试生成一个 table。但是,这里的问题是,我希望 % 用于跨列的缺失值(特别是对于分类变量),同时,我不希望在计算 p 值时包含缺失值。我正在尝试在单个代码块中执行此操作。有没有办法做到这一点,还是我应该采用传统方法?
过去三天我一直在搜索整个互联网。但是,我找不到任何适合我的情况。
PS:mutate 和 forcats 不起作用,因为它扭曲了我的 p 值。
我准备了两个解决方案,都报告了缺失数据的比例。希望其中之一适合您!
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.2'
# add % missing in new column
tbl1 <-
trial %>%
tbl_summary(
by = trt,
include = response,
type = all_dichotomous() ~ "categorical",
missing = "no"
) %>%
add_p() %>%
add_n(statistic = "{n_miss} ({p_miss}%)") %>%
modify_header(n = "**Missing**")
# prepare tbl_summary with rows for missing, then merge in p-values
tbl2 <-
trial %>%
dplyr::mutate(response = forcats::fct_explicit_na(factor(response))) %>%
tbl_summary(
by = trt,
include = response,
label = list(response = "Tumor Response")
) %>%
list(tbl1 %>% modify_column_hide(c(n, all_stat_cols()))) %>%
tbl_merge(tab_spanner = FALSE)