The cells_summary()
function
Use a portion of the countrypops
dataset to create a gt table. Add some styling to the summary data cells with tab_style()
, using cells_summary()
in the locations
argument.
countrypops |>
dplyr:: filter (country_name == "Japan" , year < 1970 ) |>
dplyr:: select (- contains ("country" )) |>
dplyr:: mutate (decade = paste0 (substr (year, 1 , 3 ), "0s" )) |>
gt (
rowname_col = "year" ,
groupname_col = "decade"
) |>
fmt_number (
columns = population,
decimals = 0
) |>
summary_rows (
groups = "1960s" ,
columns = population,
fns = list ("min" , "max" ),
fmt = ~ fmt_integer (.)
) |>
tab_style (
style = list (
cell_text (style = "italic" ),
cell_fill (color = "lightblue" )
),
locations = cells_summary (
groups = "1960s" ,
columns = population,
rows = 1
)
) |>
tab_style (
style = list (
cell_text (style = "italic" ),
cell_fill (color = "lightgreen" )
),
locations = cells_summary (
groups = "1960s" ,
columns = population,
rows = 2
)
)
population
1960s
1960
93,216,000
1961
94,055,000
1962
94,933,000
1963
95,900,000
1964
96,903,000
1965
97,952,000
1966
98,851,000
1967
99,879,000
1968
101,011,000
1969
102,219,000
min
93,216,000
max
102,219,000