在另一篇文章中,我概述了一种将总体百分比添加到gt( )
包中的表的方法(如何自动将总体百分比添加到 gt() 包中的 row_summary?)我确定的解决方案涉及对row_summary( )
函数的单独调用对于添加的每个整体行百分比。但是,如果将这个相当笨重的解决方案应用于整体组百分比,则即使是这种相当笨重的解决方案也不起作用,如下面的工作示例所示。解决方案?
# Create baseline data
set.seed(1)
df <- tibble(some_letter = sample(letters, size = 10, replace = FALSE),
some_group = sample(c("A", "B"), size = 10, replace = TRUE),
num1 = sample(100:200, size = 10, replace = FALSE),
num2 = sample(100:200, size = 10, replace = FALSE),
n = num1 + num2) %>%
mutate(across(starts_with("num"), ~(.x)/(n), .names = "pct_{col}"))
> df
# A tibble: 10 x 7
some_letter some_group num1 num2 n pct_num1 pct_num2
<chr> <chr> <int> <int> <int> <dbl> <dbl>
1 g A 194 148 342 0.567 0.433
2 j A 121 159 280 0.432 0.568
3 n B 164 200 364 0.451 0.549
4 u A 112 118 230 0.487 0.513
5 e B 125 180 305 0.410 0.590
6 s A 137 164 301 0.455 0.545
7 w B 101 175 276 0.366 0.634
8 m B 135 110 245 0.551 0.449
9 l A 180 167 347 0.519 0.481
10 b B 131 137 268 0.489 0.511
# Target: the weighted group percentages to be added to the table in gt( )
df %>% group_by(some_group) %>%
summarise_at(vars(num1, num2, n), funs(sum)) %>%
mutate(across(starts_with("num"), ~(.x)/(n), .names = "pct_{col}"))
# A tibble: 2 x 6
some_group num1 num2 n pct_num1 pct_num2
<chr> <int> <int> <int> <dbl> <dbl>
1 A 744 756 1500 0.496 0.504
2 B 656 802 1458 0.450 0.550
# Create table in gt( ), attempting to use the summary_rows( ) function to pass
# group-specific percentages for pct_num1, the result of which is that the last
# passed value is recycled across all groups...
gt(df, groupname_col = "some_group", rowname_col="some_letter") %>%
summary_rows(groups = TRUE, columns = vars(num1, num2, n), fns = list( TOTAL = "sum" ) ) %>%
summary_rows(groups = TRUE,
columns = vars(pct_num1),
fns = list(TOTAL = ~ c(0.493,0.454) )
)