Create a table for publication

You’ve used R to build a table of interesting values. Now you want to export the table as a neat and professional looking figure to include in a publication.

Step 1 - Assemble your table as a data frame. For this, you may want to use dplyr functions such as arrange(), mutate(), or summarize().

Step 2 - Pass the data frame to gt::gt(). gt() creates a professional looking, gt table version of the data frame, which you can then manipulate with other functions from the gt package.

Step 3 (Optional) - Export your table with gt::gtsave(). Provide a filename that ends with a common table format (.html, .tex, .ltx, .rtf). gtsave() will automatically save your file to that format. If you do not want to save your file to the current directory, provide a file path to a different directory, e.g.

data %>%
  gt() %>%
  gtsave(filename = "Table1.rtf", path = "figures")

Like many dplyr functions, gt() respects grouped data. If you are not counting on that, ungroup grouped data with dplyr::ungroup() before calling gt().

Example

quaker_sales describes various Quaker oatmeal products across multiple stores during one week.

quaker_sales
# A tibble: 12 × 5
   store_id product                    price profit units_sold
      <int> <chr>                      <dbl>  <dbl>      <int>
 1        8 Quaker Instant Grits 12 oz  1.87   0.2          20
 2        8 Quaker Kids Choice 14.3 oz  3.09   0.21          3
 3        8 Quick Quaker Oats 18 oz     1.89   0.2          55
 4        8 Quick Quaker Oats 42 oz     1.87   0.41        123
 5       21 Quaker Instant Grits 12 oz  1.87   0.2          12
 6       21 Quaker Kids Choice 14.3 oz  3.09   0.21          7
 7       21 Quick Quaker Oats 18 oz     1.89   0.2          21
 8       21 Quick Quaker Oats 42 oz     2.01   0.45         18
 9       44 Quaker Instant Grits 12 oz  1.87   0.2           3
10       44 Quaker Kids Choice 14.3 oz  3.09   0.21         11
11       44 Quick Quaker Oats 18 oz     1.89   0.2          53
12       44 Quick Quaker Oats 42 oz     2.28   0.52         52

We want to publish a table of the average price and total number of units sold for each product across all stores.

To begin, we load the dplyr and gt packages.

library(dplyr)
library(gt)

Next, we obtain the average price and number of units sold for each product using dplyr functions.

quaker_sales %>% 
  group_by(product) %>% 
  summarize(
    avg_price = mean(price),
    total_sold = sum(units_sold)
  )
# A tibble: 4 × 3
  product                    avg_price total_sold
  <chr>                          <dbl>      <int>
1 Quaker Instant Grits 12 oz      1.87         35
2 Quaker Kids Choice 14.3 oz      3.09         21
3 Quick Quaker Oats 18 oz         1.89        129
4 Quick Quaker Oats 42 oz         2.05        193

Finally, we pass our data frame to gt() to get a publication quality table. gt() makes a beautiful table of average price and total units sold by product.

quaker_sales %>% 
  group_by(product) %>% 
  summarize(
    avg_price = mean(price),
    total_sold = sum(units_sold)
  ) %>% 
  gt()
product avg_price total_sold
Quaker Instant Grits 12 oz 1.870000 35
Quaker Kids Choice 14.3 oz 3.090000 21
Quick Quaker Oats 18 oz 1.890000 129
Quick Quaker Oats 42 oz 2.053333 193

Publication-ready tables in SAS

Tables for publication can be created in SAS using either the TABULATE or REPORT procedure. In R, you create the table and then convert it to publication quality. However, in SAS you create the table and make it look nice all in the same step.

In SAS:

PROC TABULATE DATA = quaker_sales;
  CLASS product;
  VAR price units_sold;
  TABLE product, (price * MEAN units_sold * SUM);
RUN;

or

PROC REPORT DATA = quaker_sales;
    COLUMN product price units_sold;
    DEFINE product / GROUP;
    DEFINE price / COMPUTED MEAN;
    DEFINE units_sold / COMPUTED SUM;
RUN;

In R:

quaker_sales %>% 
  group_by(product) %>% 
  summarize(
    avg_price = mean(price),
    total_sold = sum(units_sold)
  ) %>% 
  gt()