The summary_rows()
function
Use a modified version of sp500
dataset to create a gt table with row groups and row labels. Create the summary rows labeled min
, max
, and avg
by row group (where each each row group is a week number) with summary_rows()
.
sp500 |>
dplyr:: filter (date >= "2015-01-05" & date <= "2015-01-16" ) |>
dplyr:: arrange (date) |>
dplyr:: mutate (week = paste0 ("W" , strftime (date, format = "%V" ))) |>
dplyr:: select (- adj_close, - volume) |>
gt (
rowname_col = "date" ,
groupname_col = "week"
) |>
summary_rows (
fns = list (
"min" ,
"max" ,
list (label = "avg" , fn = "mean" )
),
fmt = ~ fmt_number (., use_seps = FALSE )
)
open
high
low
close
W02
2015-01-05
2054.44
2054.44
2017.34
2020.58
2015-01-06
2022.15
2030.25
1992.44
2002.61
2015-01-07
2005.55
2029.61
2005.55
2025.90
2015-01-08
2030.61
2064.08
2030.61
2062.14
2015-01-09
2063.45
2064.43
2038.33
2044.81
min
2005.55
2029.61
1992.44
2002.61
max
2063.45
2064.43
2038.33
2062.14
avg
2035.24
2048.56
2016.85
2031.21
W03
2015-01-12
2046.13
2049.30
2022.58
2028.26
2015-01-13
2031.58
2056.93
2008.25
2023.03
2015-01-14
2018.40
2018.40
1988.44
2011.27
2015-01-15
2013.75
2021.35
1991.47
1992.67
2015-01-16
1992.25
2020.46
1988.12
2019.42
min
1992.25
2018.40
1988.12
1992.67
max
2046.13
2056.93
2022.58
2028.26
avg
2020.42
2033.29
1999.77
2014.93
Using the countrypops
dataset, let’s process that a bit before giving it to gt . We can create a summary rows with totals that appear at the top of each row group (with side = "top"
). We can define the aggregation with a list that contains parameters for the summary row label (md("**ALL**")
), the shared ID value of those rows across groups ("totals"
), and the aggregation function (expressed as "sum"
, which gt recognizes as the sum()
function). To top it all off, we’ll add background fills to the summary rows with tab_style()
.
countrypops |>
dplyr:: filter (
country_code_2 %in% c ("BR" , "RU" , "IN" , "CN" , "FR" , "DE" , "IT" , "GB" )
) |>
dplyr:: filter (year %% 10 == 0 ) |>
dplyr:: select (country_name, year, population) |>
tidyr:: pivot_wider (names_from = year, values_from = population) |>
gt (rowname_col = "country_name" ) |>
tab_row_group (
label = md ("*BRIC*" ),
rows = c ("Brazil" , "Russia" , "India" , "China" ),
id = "bric"
) |>
tab_row_group (
label = md ("*Big Four*" ),
rows = c ("France" , "Germany" , "Italy" , "United Kingdom" ),
id = "big4"
) |>
row_group_order (groups = c ("bric" , "big4" )) |>
tab_stub_indent (rows = everything ()) |>
tab_header (title = "Populations of the BRIC and Big Four Countries" ) |>
tab_spanner (columns = everything (), label = "Year" ) |>
fmt_number (n_sigfig = 3 , suffixing = TRUE ) |>
summary_rows (
fns = list (label = md ("**ALL**" ), id = "totals" , fn = "sum" ),
fmt = ~ fmt_number (., n_sigfig = 3 , suffixing = TRUE ),
side = "top"
) |>
tab_style (
locations = cells_summary (),
style = cell_fill (color = "lightblue" |> adjust_luminance (steps = + 1 ))
)
Populations of the BRIC and Big Four Countries
Year
1960
1970
1980
1990
2000
2010
2020
BRIC
ALL
1.31B
1.60B
1.94B
2.30B
2.64B
2.92B
3.16B
Brazil
73.1M
96.4M
122M
151M
176M
196M
213M
China
667M
818M
981M
1.14B
1.26B
1.34B
1.41B
India
446M
558M
697M
870M
1.06B
1.24B
1.40B
Russia
120M
130M
139M
148M
147M
143M
144M
Big Four
ALL
222M
239M
246M
251M
259M
269M
277M
Germany
72.8M
78.2M
78.3M
79.4M
82.2M
81.8M
83.2M
France
46.6M
51.7M
55.1M
58.0M
60.9M
65.0M
67.6M
United Kingdom
52.4M
55.7M
56.3M
57.2M
58.9M
62.8M
67.1M
Italy
50.2M
53.8M
56.4M
56.7M
56.9M
59.3M
59.4M