-
Notifications
You must be signed in to change notification settings - Fork 0
/
05-adding-summary-rows.Rmd
192 lines (159 loc) · 4.67 KB
/
05-adding-summary-rows.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
---
title: "Adding Summary Rows"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(gt)
library(tidyverse)
```
## Intro
There are two functions that will add rows to a **gt** table: `summary_rows()` and `grand_summary_rows()`. These are useful for adding groupwise and grand summary rows.
------
Functions in this module:
- `summary_rows()`
- `grand_summary_rows()`
------
### `summary_rows()`: Add groupwise summary rows using aggregation functions
``` r
summary_rows(
data,
groups = NULL,
columns = everything(),
fns,
missing_text = "---",
formatter = fmt_number,
...
)
```
Add summary rows to one or more row groups by using the table data and any suitable aggregation functions.
##### EXAMPLES
Use `exibble` to create a **gt** table with a stub and row groups. Get the sum of the `num` column in both groups.
```{r}
exibble %>%
gt(rowname_col = "row", groupname_col = "group") %>%
summary_rows(
groups = c("grp_a", "grp_b"), # <- `TRUE` also works to target all groups
columns = num,
fns = list(sum = ~ sum(., na.rm = TRUE))
)
```
Extend the above the example by formatting `num` values and their summary values (adding minimum and maximum summary rows as well).
```{r}
exibble %>%
gt(rowname_col = "row", groupname_col = "group") %>%
fmt_number(
columns = num,
decimals = 1
) %>%
summary_rows(
groups = TRUE,
columns = num,
fns = list(
sum = ~ sum(., na.rm = TRUE),
min = ~ min(., na.rm = TRUE),
max = ~ max(., na.rm = TRUE)
),
formatter = fmt_number,
decimals = 1
)
```
Add group summary values for the `currency` column. Use the `fmt_currency()` formatting function and re-use the summary labels (`sum`, `min`, and `max`).
```{r}
exibble %>%
gt(rowname_col = "row", groupname_col = "group") %>%
fmt_number(
columns = num,
decimals = 1
) %>%
summary_rows(
groups = TRUE,
columns = num,
fns = list(
sum = ~ sum(., na.rm = TRUE),
min = ~ min(., na.rm = TRUE),
max = ~ max(., na.rm = TRUE)
),
formatter = fmt_number,
decimals = 1
) %>%
fmt_currency(
columns = currency,
currency = "USD"
) %>%
summary_rows(
groups = TRUE,
columns = currency,
fns = list(
sum = ~ sum(., na.rm = TRUE),
min = ~ min(., na.rm = TRUE),
max = ~ max(., na.rm = TRUE)
),
formatter = fmt_currency,
currency = "USD",
missing_text = "" # <- this `missing_text` value replaces the dashes with nothing
)
```
Use `sp500` to create a **gt** table with row groups. Create summary rows (`min`, `max`, `avg`) by row group, where each each row group is a week number.
```{r}
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"
) %>%
fmt_currency(columns = everything()) %>%
summary_rows(
groups = TRUE,
columns = c(open, high, low, close),
fns = list(
min = ~min(.),
max = ~max(.),
avg = ~mean(.)
),
formatter = fmt_currency
)
```
------
### `grand_summary_rows()`: Add grand summary rows using aggregation functions
``` r
grand_summary_rows(
data,
columns = everything(),
fns,
missing_text = "---",
formatter = fmt_number,
...
)
```
Add grand summary rows to the **gt** table by using applying aggregation functions to the table data. The summary rows incorporate all of the available data, regardless of whether some of the data are part of row groups.
##### EXAMPLE
Use `sp500` to create a **gt** table with row groups. Create grand summary rows (`min`, `max`, `avg`) for the table.
```{r}
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"
) %>%
fmt_currency(columns = everything()) %>%
grand_summary_rows(
columns = c(open, high, low, close),
fns = list(
min = ~min(.),
max = ~max(.),
avg = ~mean(.)),
formatter = fmt_currency
)
```
------
### SUMMARY
1. The `summary_rows()` and `grand_summary_rows()` functions allow for calculation and insertion of summary rows, either per group or for all rows in the table.
2. You might call these functions several times to build up summary rows; use the same labels to line up entries across columns.
3. The functions are not very easy to use, don't feel bad using `?summary_rows` or `?grand_summary_rows` and working from an example.