---
title: "latex-08-using_summary_rows"
output: 
  - pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(gt)
library(tidyverse)
```

Create a display table with summary rows.

```{r}
# Input table
tbl <-
  dplyr::tribble(
    ~group, ~rowname, ~value,  ~value_2,
    "A",        "1",      NA,      260.1,
    "A",        "2",      184.3,   84.4,
    "A",        "3",      342.3,   126.3,
    "A",        "4",      234.9,   NA,
    "B",        "1",      190.9,   832.5,
    "B",        "2",      743.3,   281.2,
    "B",        "3",      252.3,   732.5,
    "B",        "4",      344.7,   NA,
    "C",        "1",      197.2,   818.0,
    "C",        "2",      284.3,   394.4
  )
```

Create a display table with summary rows.

```{r}
gt(tbl, groupname_col = "group") %>%
  summary_rows(
    groups = c("A", "C"),
    columns = value,
    fns = list(
      ~mean(., na.rm = TRUE),
      ~sum(., na.rm = TRUE),
      ~sd(., na.rm = TRUE))) %>%
  summary_rows(
    groups = c("B"),
    columns = value_2,
    fns = list(
      ~sum(., na.rm = TRUE))) %>%
  sub_missing(columns = c(value, value_2))
```
