The cols_merge_n_pct()
function
Use pizzaplace
to create a gt table that displays the counts and percentages of the top 3 pizzas sold by pizza category in 2015. The cols_merge_n_pct()
function is used to merge the n
and frac
columns (and the frac
column is formatted using fmt_percent()
).
pizzaplace |>
dplyr:: group_by (name, type, price) |>
dplyr:: summarize (
n = dplyr:: n (),
frac = n/ nrow (pizzaplace),
.groups = "drop"
) |>
dplyr:: arrange (type, dplyr:: desc (n)) |>
dplyr:: group_by (type) |>
dplyr:: slice_head (n = 3 ) |>
gt (
rowname_col = "name" ,
groupname_col = "type"
) |>
fmt_currency (price) |>
fmt_percent (frac) |>
cols_merge_n_pct (
col_n = n,
col_pct = frac
) |>
cols_label (
n = md ("*N* (%)" ),
price = "Price"
) |>
tab_style (
style = cell_text (font = "monospace" ),
locations = cells_stub ()
) |>
tab_stubhead (md ("Cat. and \\ nPizza Code" )) |>
tab_header (title = "Top 3 Pizzas Sold by Category in 2015" ) |>
tab_options (table.width = px (512 ))
Top 3 Pizzas Sold by Category in 2015
Cat. and \nPizza Code
Price
N (%)
chicken
thai_ckn
$20.75
1410 (2.84%)
southw_ckn
$20.75
1016 (2.05%)
bbq_ckn
$20.75
992 (2.00%)
classic
big_meat
$12.00
1914 (3.86%)
classic_dlx
$16.00
1181 (2.38%)
hawaiian
$10.50
1020 (2.06%)
supreme
spicy_ital
$20.75
1109 (2.24%)
ital_supr
$16.50
941 (1.90%)
sicilian
$12.25
751 (1.51%)
veggie
five_cheese
$18.50
1409 (2.84%)
four_cheese
$17.95
1316 (2.65%)
mexicana
$20.25
867 (1.75%)