The extract_summary() function

Use a modified version of sp500 the dataset to create a gt table with row groups and row labels. Create summary rows labeled as min, max, and avg for every row group with summary_rows(). Then, extract the summary rows as a list object.

summary_extracted <-
  sp500 |>
  dplyr::filter(date >= "2015-01-05" & date <="2015-01-30") |>
  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(
    groups = everything(),
    columns = c(open, high, low, close),
    fns = list(
      min = ~min(.),
      max = ~max(.),
      avg = ~mean(.)
    ),
  ) |>
  extract_summary()

summary_extracted
$summary_df_data_list
$summary_df_data_list$W02
# A tibble: 3 × 9
  group_id row_id rowname  date  open  high   low close  week
  <chr>    <chr>  <chr>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 W02      min    min        NA 2006. 2030. 1992. 2003.    NA
2 W02      max    max        NA 2063. 2064. 2038. 2062.    NA
3 W02      avg    avg        NA 2035. 2049. 2017. 2031.    NA

$summary_df_data_list$W03
# A tibble: 3 × 9
  group_id row_id rowname  date  open  high   low close  week
  <chr>    <chr>  <chr>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 W03      min    min        NA 1992. 2018. 1988. 1993.    NA
2 W03      max    max        NA 2046. 2057. 2023. 2028.    NA
3 W03      avg    avg        NA 2020. 2033. 2000. 2015.    NA

$summary_df_data_list$W04
# A tibble: 3 × 9
  group_id row_id rowname  date  open  high   low close  week
  <chr>    <chr>  <chr>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 W04      min    min        NA 2020. 2029. 2004. 2023.    NA
2 W04      max    max        NA 2063. 2065. 2051. 2063.    NA
3 W04      avg    avg        NA 2035. 2049. 2023. 2042.    NA

$summary_df_data_list$W05
# A tibble: 3 × 9
  group_id row_id rowname  date  open  high   low close  week
  <chr>    <chr>  <chr>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 W05      min    min        NA 2002. 2023. 1989. 1995.    NA
2 W05      max    max        NA 2050. 2058. 2041. 2057.    NA
3 W05      avg    avg        NA 2030. 2039. 2009. 2021.    NA

Use the summary list to make a new gt table. The key thing is to use dplyr::bind_rows() and then pass the tibble to gt().

summary_extracted |>
  unlist(recursive = FALSE) |>
  dplyr::bind_rows() |>
  gt(groupname_col = "group_id") |>
  cols_hide(columns = row_id)
date open high low close week
W02
min NA 2005.550 2029.610 1992.440 2002.610 NA
max NA 2063.450 2064.430 2038.330 2062.140 NA
avg NA 2035.240 2048.562 2016.854 2031.208 NA
W03
min NA 1992.250 2018.400 1988.120 1992.670 NA
max NA 2046.130 2056.930 2022.580 2028.260 NA
avg NA 2020.422 2033.288 1999.772 2014.930 NA
W04
min NA 2020.190 2028.940 2004.490 2022.550 NA
max NA 2062.980 2064.620 2050.540 2063.150 NA
avg NA 2034.557 2048.707 2023.362 2042.410 NA
W05
min NA 2002.450 2023.320 1989.180 1994.990 NA
max NA 2050.420 2057.620 2040.970 2057.090 NA
avg NA 2030.484 2039.186 2008.986 2021.008 NA