The cols_move_to_end() function

For this example, we’ll use a portion of the countrypops dataset to create a simple gt table. Let’s move the year column, which is the middle column, to the end of the column series with cols_move_to_end().

countrypops |>
  dplyr::select(-contains("code")) |>
  dplyr::filter(
    country_name == "Benin",
    year %in% 2017:2021
  ) |>
  gt() |>
  cols_move_to_end(columns = year)
country_name population year
Benin 11596779 2017
Benin 11940683 2018
Benin 12290444 2019
Benin 12643123 2020
Benin 12996895 2021

We can also move multiple columns at a time. With the same countrypops-based table, let’s move both the year and country_name columns to the end of the column series.

countrypops |>
  dplyr::select(-contains("code")) |>
  dplyr::filter(
    country_name == "Benin",
    year %in% 2017:2021
  ) |>
  gt() |>
  cols_move_to_end(columns = c(year, country_name))
population year country_name
11596779 2017 Benin
11940683 2018 Benin
12290444 2019 Benin
12643123 2020 Benin
12996895 2021 Benin