The cols_merge_n_pct() function

Using a summarized version of the pizzaplace dataset, let’s 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::count(name, type, price, sort = TRUE) |>
  dplyr::mutate(frac = prop.table(n)) |>
  dplyr::slice_max(n, n = 3, by = type) |>
  dplyr::arrange(type) |>
  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%)