'R - GT Package systematically calculate summary_row percentages
I have a table that I made using the GT package, but I cannot get summary_row percentage to calculate correctly (green row).
Is there a way to systematically take the sum of the column and divide by the Total N. So for example, Female percent should be 8/11 (73%).
summary_rows(
groups = TRUE,
columns = vars(total, NRA, FN, HN, BN, AIN, AN, NHN, WN, TN, MN, UN), #N columns
fns = list(TOTAL = "sum"),
decimals = 0,
use_seps = TRUE
) %>%
summary_rows(
groups = TRUE,
columns = vars(FP,NRAP,HP,BP, AIP, AP, NHP, WP, TP, MP, UP), #Percentages columns
fns = list(TOTAL = ~ sum(.)/sum(total)),
decimals = 0,
formatter = fmt_percent,
use_seps = TRUE
) %>%
In the code above, the first set of summary_rows is for computing N, which is easy because its a simple sum. This is my attempt at getting summary row percentages. fns = list(TOTAL = ~ sum(.)/sum(total)),
I have tried looking at similar questions/answers and couldnt find quite what I was looking for.
Thanks
Edit - asked for full code
t2= ds %>% group_by(Division, Department) %>%
summarise(Category = "University Total",
Division = "University",
Total = round(n(),digits = 2),
FN = n_distinct(Pernr[Gender == "Female"]),
FP = n_distinct(Pernr[Gender == "Female"])/Total,
NRA = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "NRA"]),
NRAP = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "NRA"])/Total,
HN = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "Hispanic"]),
HP = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "Hispanic"])/Total,
BN = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "Black or African American"]),
BP = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "Black or African American"])/Total,
AIN = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "American Indian or Alaska Native"]),
AIP = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "American Indian or Alaska Native"])/Total,
AN = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "Asian"]),
AP = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "Asian"])/Total,
NHN = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "Native Hawaiian or Other Pacific Islander"]),
NHP = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "Native Hawaiian or Other Pacific Islander"])/Total,
WN = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "White"]),
WP = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "White"])/Total,
TN = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "Two or more races"]),
TP = n_distinct(Pernr[`i_OLD_FCR_Race Eth` == "Two or more races"])/Total,
MN = n_distinct(Pernr[Minority == "Minority"]),
MP = n_distinct(Pernr[Minority == "Minority"])/Total,
UN = n_distinct(Pernr[URM == "URM"]),
UP = n_distinct(Pernr[URM == "URM"])/Total
)
gtfcr = t2 %>%
gt(rowname_col="Department") %>%
tab_header(
title = "Table 2: FCR Fall 2021"
) %>%
tab_spanner(
label = md("*Female*"),
columns = vars(FN, FP)
) %>%
tab_spanner(
label = md("*NRA*"),
columns = vars(NRA, NRAP)
) %>%
tab_spanner(
label = md("*Hispanic*"),
columns = vars(HN, HP)
) %>%
tab_spanner(
label = md("*Black*"),
columns = vars(BN, BP)
) %>%
tab_spanner(
label = md("*Amer Ind*"),
columns = vars(AIN, AIP)
) %>%
tab_spanner(
label = md("*Asian*"),
columns = vars(AN, AP)
) %>%
tab_spanner(
label = md("*Nat Haw*"),
columns = vars(NHN, NHP)
) %>%
tab_spanner(
label = md("*White*"),
columns = vars(WN, WP)
) %>%
tab_spanner(
label = md("*Two or More*"),
columns = vars(TN, TP)
) %>%
tab_spanner(
label = md("*Minority*"),
columns = vars(MN,MP)
) %>%
tab_spanner(
label = md("*URM*"),
columns = vars(UN,UP)
) %>%
fmt_percent(
columns = vars(NRAP,FP,HP,BP, AIP, AP, NHP, WP, TP, MP, UP),
decimals = 0
) %>%
cols_align(align = "center", columns = TRUE) %>%
data_color(
columns = vars(total),
colors = scales::col_numeric(
palette = "grey86"#paletteer::paletteer_d(palette = "nord::")
%>% as.character(),
domain = NULL
),
alpha = 0.8
) %>%
summary_rows(
groups = TRUE,
columns = vars(total, NRA, FN, HN, BN, AIN, AN, NHN, WN, TN, MN, UN), #N columns
fns = list(TOTAL = "sum"),
decimals = 0,
use_seps = TRUE
) %>%
summary_rows(
groups = TRUE,
columns = vars(FP,NRAP,HP,BP, AIP, AP, NHP, WP, TP, MP, UP), #Percentages columns
fns = list(TOTAL = ~ sum(.)/sum(total)),
decimals = 0,
formatter = fmt_percent,
use_seps = TRUE
) %>%
tab_options(
summary_row.background.color = "#ACEACE80",
grand_summary_row.background.color = "#990000",
row_group.background.color = "#FFEFDB80",
heading.background.color = "#EFFBFC",
column_labels.background.color = "#EFFBFC",
stub.background.color = "#EFFBFC",
table.font.color = "#323232",
table_body.hlines.color = "#989898",
table_body.border.top.color = "#989898",
heading.border.bottom.color = "#989898",
row_group.border.top.color = "#989898",
row_group.border.bottom.style = "none",
stub.border.style = "dashed",
stub.border.color = "#989898",
stub.border.width = "1px",
summary_row.border.color = "#989898",
table.width = "60%"
) %>%
cols_label(
NRA = "N",
NRAP = "%",
FN = "N",
FP = "%",
HN = "N",
HP = "%",
BN = "N",
BP = "%",
AIN = "N",
AIP = "%",
AN = "N",
AP = "%",
NHN = "N",
NHP = "%",
WN = "N",
WP = "%",
TN = "N",
TP = "%",
MN = "N",
MP = "%",
UN = "N",
UP = "%"
) %>%
opt_all_caps()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

