In a separate post I outline a method for adding overall percentages to a table in the gt( ) package (How can you automate the addition of overall percentages to the row_summary in the gt( ) package?) The solution I identified involved a separate invocation of the row_summary( ) function for each overall row percentage being added. But even this rather clunky solution doesn't work if applied to overall group percentages, as illustrated through the worked example below. Solutions?
# 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) )
)