# NOT RUN {
# 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()`)
tab_1 <-
pizzaplace %>%
dplyr::group_by(name, type, price) %>%
dplyr::summarize(
n = dplyr::n(),
frac = n/nrow(.),
.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))
# }
Run the code above in your browser using DataLab