data %>%
gt() %>%
grand_summary_rows (
columns = everything(),
fns = list(
average = "mean",
total = "sum"
)
)Add summary rows to the bottom of a table for publication
You are making a table for publication and want to add summary rows to the end of the table with statistics, such as mean, sum, or maximum, for one or more columns.
Step 1 - Start with a data frame that contains the values that you want to display in the arrangement that you want to display them.
Step 2 - Use gt::gt() to turn your data frame into an editable, professional looking table.
Step 3 - Add summary rows with gt::grand_summary_rows(). Specify which columns to summarize, and which functions (fns) to use.
Include function arguments
You can use the anonymous function notation associated with purrr:map() to set arguments for your summary functions. Pass the function as a call prefixed with ~. Place a . where the column vector should go.
fns = list(
average = ~mean(., na.rm = TRUE),
total = ~sum(., na.rm = TRUE),
std_dev = ~sd(., na.rm = TRUE)
)Example
CA_vaccines describes the number of doses of COVID-19 vaccines allocated to California for five months in 2021.
CA_vaccines# A tibble: 5 × 2
month doses
<ord> <dbl>
1 January 3800750
2 February 5083030
3 March 9642200
4 April 8366900
5 May 10254000
We’d like to turn this table into a figure and add a row that displays the total number of doses.
We use gt() to create the figure, and then add a summary row with grand_summary_rows(), taking advantage of several gt formatting options. By default, grand_summary_row() will summarize every column except for the row names.
library(gt)
CA_vaccines %>%
gt(rowname_col = "month") %>%
grand_summary_rows(
fns = "sum",
formatter = fmt_number,
decimals = 0
) %>%
tab_options(grand_summary_row.background.color = "lightyellow")Warning: Since gt v0.9.0, the `formatter` argument (and associated `...`) has been
deprecated.
• Please use the `fmt` argument to provide formatting directives.
This warning is displayed once every 8 hours.
| doses | |
|---|---|
| January | 3800750 |
| February | 5083030 |
| March | 9642200 |
| April | 8366900 |
| May | 10254000 |
| sum | 37,146,880 |
Add grand summary in SAS
summary_rows() is similar to SAS’s REPORT procedure with the RBREAK statement or calculating statistics in the SQL procedure. Notably, the REPORT procedure only calculates the sum per group and the SQL procedure adds the summary values into new columns rather than rows.
In SAS:
PROC REPORT DATA=data;
COLUMN group_col row_col col_A;
RBREAK AFTER / SUMMARIZE;
RUN;or
PROC SQL;
SELECT *,
SUM(col_A) AS grand_total
FROM data;
QUIT;In R:
data %>%
gt(
rowname_col = "row_col",
groupname_col = "group_col"
) %>%
grand_summary_rows(
grand_total = col_A,
fns = "sum"
)