admin管理员组文章数量:1304249
I want to use 2 times the digits= argument in tbl_summary but I can't. Once it would be like this to specify the number of decimal digits
digits = list(all_categorical() ~ c(0,1))
and the second one to indicate that there is no space as a separator for big numbers
digits = ~label_style_number(big.mark = "")
But it doesn't work in the same code
df %>% %>%
mutate_all(as.factor) %>%
tbl_summary(statistic = list(all_categorical() ~ "{n} ({p})"),
digits = list(all_categorical() ~ c(0,1)),
digits = list(a ~ label_style_number(big.mark = "")))
Error in tbl_summary(., statistic = list(all_categorical() ~ “{n} ({p}”), :
formal argument “digits” matches multiple specified arguments.
also tried this
df %>%
mutate_all(as.factor) %>%
tbl_summary(
by= a,
missing = "always",
statistic = list(all_categorical() ~ "{n} ({p})"),
sort = all_categorical(FALSE) ~ "frequency",
digits = list(all_categorical() ~ c(0, 1),
b ~ label_style_number(big.mark = "")))
but it returns a 0 in the decimals for all values
How can I have both things?
This is the minimal reproducible example I use:
df<-
tibble(a= sample(c('red', 'blue', 'pink'),
size= 10000,
replace= T),
b= sample(c('car', 'bike', 'boat'),
size= 10000,
replace= T))
I want to use 2 times the digits= argument in tbl_summary but I can't. Once it would be like this to specify the number of decimal digits
digits = list(all_categorical() ~ c(0,1))
and the second one to indicate that there is no space as a separator for big numbers
digits = ~label_style_number(big.mark = "")
But it doesn't work in the same code
df %>% %>%
mutate_all(as.factor) %>%
tbl_summary(statistic = list(all_categorical() ~ "{n} ({p})"),
digits = list(all_categorical() ~ c(0,1)),
digits = list(a ~ label_style_number(big.mark = "")))
Error in tbl_summary(., statistic = list(all_categorical() ~ “{n} ({p}”), :
formal argument “digits” matches multiple specified arguments.
also tried this
df %>%
mutate_all(as.factor) %>%
tbl_summary(
by= a,
missing = "always",
statistic = list(all_categorical() ~ "{n} ({p})"),
sort = all_categorical(FALSE) ~ "frequency",
digits = list(all_categorical() ~ c(0, 1),
b ~ label_style_number(big.mark = "")))
but it returns a 0 in the decimals for all values
How can I have both things?
This is the minimal reproducible example I use:
df<-
tibble(a= sample(c('red', 'blue', 'pink'),
size= 10000,
replace= T),
b= sample(c('car', 'bike', 'boat'),
size= 10000,
replace= T))
Share
asked Feb 10 at 18:44
mala famamala fama
332 bronze badges
2 Answers
Reset to default 0There are two ways to do this. The first looks most like the code in your example, and the second I find a bit easier to use.
- The digits argument can accept integers, but these are aliases that are converted into functions that round and convert the result to a character. You cannot combine
big.mark
specification with this shortcut. In this case, you'll need to pass functions (functions are returned from thelabel_*()
functions) with thebig.mark
specified.
library(gtsummary)
rep(list(trial), 15) |>
dplyr::bind_rows() |>
tbl_summary(
include = grade,
digits = all_categorical() ~ list(p = label_style_percent(big.mark = ""),
n = label_style_number(big.mark = ""))
) |>
modify_header(all_stat_cols() ~ "**N = {style_number(n, big.mark = '')}**") |>
as_kable() # convert to markdown so table renders on SO
Characteristic | N = 3000 |
---|---|
grade | |
I | 1020 (34%) |
II | 1020 (34%) |
III | 960 (32%) |
Created on 2025-02-10 with reprex v2.1.1
The {gtsummary} package supports translations into many languages (~12 I believe). When we set the translation language, we can also set the big mark and the decimal mark to match that country's convention. Once set, all numbers printed with gtsummary will follow that convention.
In the example below, I set the language to English (the default, so no change there), and big.mark=""
.
library(gtsummary)
theme_gtsummary_language(language = "en", big.mark = "")
#> Setting theme "language: en"
rep(list(trial), 15) |>
dplyr::bind_rows() |>
tbl_summary(include = grade) |>
as_kable() # convert to markdown so table renders on SO
Characteristic | N = 3000 |
---|---|
grade | |
I | 1020 (34%) |
II | 1020 (34%) |
III | 960 (32%) |
Created on 2025-02-10 with reprex v2.1.1
Hope that helps!
library(gtsummary)
library(dplyr)
# Example data
df <- tibble(
a = sample(c('red', 'blue', 'pink'), size = 10000, replace = TRUE),
b = sample(c('car', 'bike', 'boat'), size = 10000, replace = TRUE)
)
# Apply tbl_summary with custom formatting
df %>%
mutate_all(as.factor) %>%
tbl_summary(
by = a,
missing = "always",
statistic = list(all_categorical() ~ "{n} ({p})"),
sort = all_categorical(FALSE) ~ "frequency",
digits = list(all_categorical() ~ c(0, 1)) # Round percentages to 1 decimal place
) %>%
# Use modify_fmt_fun to apply big.mark formatting to summary statistics columns
modify_fmt_fun(
update = starts_with("stat_") ~ style_number(big.mark = "")
)
本文标签: rHow can I use 2 times the same argument in tblsummary()Stack Overflow
版权声明:本文标题:r - How can I use 2 times the same argument in tbl_summary() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741697562a2393095.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论