我只想为百分比列添加小数
#分离列库(gtsummary) 库(tidyverse)
tbl <-
c("{n}", "{p}%") %>% # iterate over these two statistics
# build tbl_summary using each of the stats
map(
~trial %>%
select(response, grade) %>%
tbl_summary(
statistic = all_categorical() ~ .x,
missing = "ifany",
digits = list(
all_categorical() ~ 1,
all_continuous() ~ 0
),
missing_text = "(Missing)"
)
) %>%
# merge the two tables together
tbl_merge() %>%
# some formatting to make it cute
modify_spanning_header(everything() ~ NA) %>%
modify_footnote(everything() ~ NA) %>%
modify_header(list(stat_0_1 ~ "**n / N**", stat_0_2 ~ "**percent**"))
Run Code Online (Sandbox Code Playgroud)
您可以有条件地添加一个digits=值,具体取决于我们是汇总百分比还是计数。下面举例!
library(gtsummary)
tbl <-
c("{n}", "{p}%") %>% # iterate over these two statistics
# build tbl_summary using each of the stats
purrr::map(
~trial %>%
select(response, grade) %>%
tbl_summary(
statistic = all_categorical() ~ .x,
missing = "ifany",
digits = list(
all_categorical() ~ ifelse(.x == "{p}%", 1, 0),
all_continuous() ~ 0
),
missing_text = "(Missing)"
)
) %>%
# merge the two tables together
tbl_merge() %>%
# some formatting to make it cute
modify_spanning_header(everything() ~ NA) %>%
modify_footnote(everything() ~ NA) %>%
modify_header(list(stat_0_1 ~ "**n / N**", stat_0_2 ~ "**percent**"))
Run Code Online (Sandbox Code Playgroud)
由reprex 包于 2021 年 12 月 7 日创建(v2.0.1)