The fmt_index() function

Using a summarized version of the towny dataset, let’s create a gt table. Here, fmt_index() is used to transform incremental integer values into capitalized letters (in the ranking column). With cols_merge() that formatted column of "A" to "E" values is merged with the census_div column to create an indexed listing of census subdivisions, here ordered by increasing total municipal population.

towny |>
  dplyr::select(name, csd_type, census_div, population_2021) |>
  dplyr::group_by(census_div) |>
  dplyr::summarize(
    population = sum(population_2021),
    .groups = "drop_last"
  ) |>
  dplyr::slice_min(population, n = 5) |>
  dplyr::mutate(ranking = dplyr::row_number(), .before = 0) |>
  gt() |>
  fmt_integer() |>
  fmt_index(columns = ranking, pattern = "{x}.") |>
  cols_merge(columns = c(ranking, census_div)) |>
  cols_align(align = "left", columns = ranking) |>
  cols_label(
    ranking = md("Census  \\nSubdivision"),
    population = md("Population  \\nin 2021")
  ) |>
  tab_header(title = md("The smallest  \\ncensus subdivisions")) |>
  tab_options(table.width = px(325))
The smallest \ncensus subdivisions
Census \nSubdivision Population \nin 2021
A. Manitoulin 8,906
B. Rainy River 15,769
C. Sudbury 18,606
D. Haliburton 20,571
E. Prince Edward 25,704