Create groups of rows in a table for publication

You are making a table for publication, and you want to organize the rows of the table into groups based on the values of one or more variables.

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 - Pass you group variable(s) to groupname_col as character strings. gt() will create a separate group of rows for each value or combination of values in these columns.

data %>% 
  gt(groupname_col = c("col1", "col2"))

Grouped data

gt() automatically creates groups of rows for data grouped with dplyr::group_by(), e.g.

data %>% 
  group_by(col1, col2) %>%
  gt()

Example

CA_vaccines describes the number of doses of COVID-19 vaccine allocated to California for three months in 2021.

CA_vaccines
# A tibble: 9 × 3
  month vaccine total_doses
  <chr> <chr>         <dbl>
1 March Janssen      632700
2 March Moderna     3803000
3 March Pfizer      5206500
4 April Janssen      640300
5 April Moderna     3304000
6 April Pfizer      4422600
7 May   Janssen      155400
8 May   Moderna     4342200
9 May   Pfizer      5756400

When we publish this data, we’d like to group the rows into separate groups for each vaccine type.

To do this, we specify vaccine as a groupname column when we convert our table for publication with gt().

library(gt)
CA_vaccines %>% 
  gt(
    groupname_col = "vaccine", 
    rowname_col = "month"
  )
total_doses
Janssen
March 632700
April 640300
May 155400
Moderna
March 3803000
April 3304000
May 4342200
Pfizer
March 5206500
April 4422600
May 5756400

Create row groups manually

If you need more control over row groups, consider creating groups manually with gt::tab_row_group().

Group rows in SAS

Creating row groups in gt() is equivalent to SAS’s REPORT procedure with a GROUP defined.

In SAS:

PROC REPORT DATA = data;
  COLUMN col_A col_B col_c;
  DEFINE col_A / GROUP;
  DEFINE col_B / DISPLAY;
  DEFINE col_C / DISPLAY;
RUN;

In R:

data %>% 
  gt(
    groupname_col = "col_A"
  )