为什么函数 "round" 对 p 值的数字不起作用?以及如何使用 "gtsummary" 调整汇总表中的百分比位数?
Why the function "round" does not work on the digits of p-value? and How to adjust the digits of percentage in the summary tables using "gtsummary"?
当我创建“自定义 pvalue 函数到 add_p()”时,我尝试调整 p 值的位数,但发现函数 "round" 不起作用。 (见代码"result$p <- round(result$p, 3)")
此外,我发现我无法更改摘要中计数百分比的数字 table。
ttest1 <- function(data, variable, by, ...) {
result <- list()
result$p <- stats::t.test(data[[variable]] ~ data[[by]])$statistic
result$p <- round(result$p, 3)
result$test <- "t test"
result
}
ttest2 <- function(data, variable, by, ...) {
result <- list()
result$p <- stats::t.test(data[[variable]] ~ data[[by]])$p.value
result$p <- round(result$p, 3)
result$test <- "t test"
result
}
add_p_ex1 <-trial[c("age","grade", "response", "trt")] %>%
tbl_summary(by = trt,
statistic = list(all_continuous() ~ "{mean} ± {sd}",
all_categorical() ~ "{n} ({p})"),
digits = list(all_continuous() ~ c(2, 2))) %>%
add_p(test = list(all_continuous() ~ "ttest1", all_categorical() ~ "chisq1")) %>%
modify_header(p.value = md("**t/X2**"))
add_p_ex2 <-
tbl_summary(by = trt,
statistic = list(all_continuous() ~ "{mean} ± {sd}",
all_categorical() ~ "{n} ({p})"),
digits = list(all_continuous() ~ c(2, 2))) %>%
add_p(test = list(all_continuous() ~ "ttest1", all_categorical() ~ "chisq2"))
tbl_merge(list(add_p_ex1, add_p_ex2)) %>%
as_gt(include = -tab_spanner) %>%
cols_hide(columns = vars(stat_1_2, stat_2_2))
首先,我能否称赞一下您构建的table:我印象深刻!
要更改 table 中 p 值的格式,请使用 add_p(pvalue_fmt=)
参数传递一个函数。该函数应采用数字向量,return formatted/rounded 字符向量。
要修改格式百分比,请使用 tbl_summary(digits=)
参数。
示例如下!
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.4.2'
tbl <-
trial %>%
dplyr::select(trt, age, grade) %>%
tbl_summary(
by = trt,
# show percentages to 1 decimal place
digits = all_categorical() ~ c(0, 1)
) %>%
# rounding p-values to 3 decimal places
add_p(pvalue_fun = function(x) style_number(x, digits = 3))
由 reprex package (v2.0.0)
于 2021-07-23 创建
当我创建“自定义 pvalue 函数到 add_p()”时,我尝试调整 p 值的位数,但发现函数 "round" 不起作用。 (见代码"result$p <- round(result$p, 3)")
此外,我发现我无法更改摘要中计数百分比的数字 table。
ttest1 <- function(data, variable, by, ...) {
result <- list()
result$p <- stats::t.test(data[[variable]] ~ data[[by]])$statistic
result$p <- round(result$p, 3)
result$test <- "t test"
result
}
ttest2 <- function(data, variable, by, ...) {
result <- list()
result$p <- stats::t.test(data[[variable]] ~ data[[by]])$p.value
result$p <- round(result$p, 3)
result$test <- "t test"
result
}
add_p_ex1 <-trial[c("age","grade", "response", "trt")] %>%
tbl_summary(by = trt,
statistic = list(all_continuous() ~ "{mean} ± {sd}",
all_categorical() ~ "{n} ({p})"),
digits = list(all_continuous() ~ c(2, 2))) %>%
add_p(test = list(all_continuous() ~ "ttest1", all_categorical() ~ "chisq1")) %>%
modify_header(p.value = md("**t/X2**"))
add_p_ex2 <-
tbl_summary(by = trt,
statistic = list(all_continuous() ~ "{mean} ± {sd}",
all_categorical() ~ "{n} ({p})"),
digits = list(all_continuous() ~ c(2, 2))) %>%
add_p(test = list(all_continuous() ~ "ttest1", all_categorical() ~ "chisq2"))
tbl_merge(list(add_p_ex1, add_p_ex2)) %>%
as_gt(include = -tab_spanner) %>%
cols_hide(columns = vars(stat_1_2, stat_2_2))
首先,我能否称赞一下您构建的table:我印象深刻!
要更改 table 中 p 值的格式,请使用 add_p(pvalue_fmt=)
参数传递一个函数。该函数应采用数字向量,return formatted/rounded 字符向量。
要修改格式百分比,请使用 tbl_summary(digits=)
参数。
示例如下!
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.4.2'
tbl <-
trial %>%
dplyr::select(trt, age, grade) %>%
tbl_summary(
by = trt,
# show percentages to 1 decimal place
digits = all_categorical() ~ c(0, 1)
) %>%
# rounding p-values to 3 decimal places
add_p(pvalue_fun = function(x) style_number(x, digits = 3))