Skip to contents

The apa7 package makes tables in the style of the Publication Manual of the American Psychological Association (7th edition). Most of its functions take a fitted model or a data frame and return a finished table: regression coefficients, ANOVA source tables, descriptive statistics, correlations, reliability, factor loadings, marginal means, meta-analyses, and more. It also has functions for formatting numbers and statistics in APA style, in tables and in text.

Because apa7 is built atop the aptly named flextable package, apa7 tables are easily customized. If the initial output fails to give you exactly what you need, apa7 functions do not box you in. After apa7’s initial processing, you can perform additional tweaks to the data before it is converted to a table. After the table is created, you can add any polishing touches (e.g., bolding, borders, and footnotes) using a series of flextable functions. Thus, apa7 removes most the tedious processing steps one would otherwise need to perform on every table, but allows you to focus on the final finishing touches that tables often need.

The apa7 package was specifically designed to work with apaquarto, a Quarto extension that generates APA manuscripts in .docx, .pdf, and .html formats. However, it can be used in whatever context you wish (e.g., in regular Quarto or in R Markdown). Even if you are writing directly in MS Word, each table can be saved to MS Word’s .docx format with flextable’s save_as_docx function.

Example: Report a Regression Analysis

The sat.act data set in the psych package has the self-reported ACT and SAT scores of 700 people. Give the variables names that will read well in a table, and keep the people with all of their scores:

d_sat <- psych::sat.act |>
  na.omit() |>
  dplyr::transmute(
    Gender = factor(gender, levels = 1:2, labels = c("Men", "Women")),
    Age = age,
    ACT,
    Verbal = SATV,
    Quantitative = SATQ
  )

Fit a model as you usually would, and pass it to apa_parameters. Here, ACT scores are predicted from SAT Verbal and SAT Quantitative scores:

fit <- lm(ACT ~ Verbal + Quantitative, data = d_sat)
apa_parameters(fit)

Variable

B

SE

β

t(684)

p

Constant

10.57

0.85

.00

12.37

<.001

Verbal

 0.01

0.00

.31

 8.05

<.001

Quantitative

 0.02

0.00

.39

 9.99

<.001

In a Quarto or R Markdown document, the result is displayed as an APA-style table. In the console, it prints as a tibble, so you can look at it or change it with dplyr functions before it is displayed. For example, you might want to use filter to drop rows or mutate to customize the display of a one or more variables.

To customize the table, or to save it to a Word file, pass it to apa_flextable, which returns a flextable. Any flextable function can change it further (e.g., flextable::bold can selectively bold cells):

apa_parameters(fit) |>
  apa_flextable(font_size = 10, table_width = .6) 

Variable

B

SE

β

t(684)

p

Constant

10.57

0.85

.00

12.37

<.001

Verbal

 0.01

0.00

.31

 8.05

<.001

Quantitative

 0.02

0.00

.39

 9.99

<.001

Table Notes

Every table records how it was made, and apa_note turns that record into the text of a table note: what the abbreviations stand for, which tests were used, how p-values were adjusted, and what the stars mean.

apa_parameters(fit, starred = "p") |>
  apa_note()
#> [1] "*N* = 687. &beta; = standardized coefficient. ^\\* ^*p* ≤ .05. ^\\*\\* ^*p* ≤ .01. ^\\*\\*\\* ^*p* ≤ .001."

apa_flextable(note = TRUE) puts the note below the table:

apa_parameters(fit, starred = "p") |>
  apa_flextable(note = TRUE)

Variable

B

SE

β

t(684)

p

Constant

10.57

0.85

.00

12.37

<.001***

Verbal

 0.01

0.00

.31

 8.05

<.001***

Quantitative

 0.02

0.00

.39

 9.99

<.001***

Note. N = 687. β = standardized coefficient. * p ≤ .05. ** p ≤ .01. *** p ≤ .001.

When using apaquarto, put the note in the apa-note chunk option instead, as described in the table notes section of the regression article. Some notes cite the articles that describe a method; apa7_write_bib writes their references so that the citations appear in your reference list (see citations in notes).

A Tour of the Table Functions

Each family of tables has an article with examples and options.

Analysis Functions Article
Regression (linear, logistic, count, ordinal, multinomial) apa_parameters, apa_performance, apa_performance_comparison, apa_collinearity Regression
Multilevel models (lme4, glmmTMB, nlme) apa_parameters, apa_performance Multilevel models
ANOVA apa_anova ANOVA
Marginal means, contrasts, simple slopes apa_means, apa_contrasts, apa_slopes Marginal means
t tests, Wilcoxon tests, correlation tests, chi-square tests apa_htest Hypothesis tests
Descriptive statistics (“Table 1”) apa_descriptives Descriptive statistics
Correlation matrices apa_cor Correlations
Contingency tables apa_chisq Contingency tables
Reliability and item analysis apa_reliability Reliability
Factor analysis apa_loadings, apa_factor_cor Factor analysis
Structural equation models (lavaan) apa_parameters, apa_performance, apa_loadings Structural equation models
Meta-analysis (metafor) apa_parameters, apa_performance, apa_publication_bias Meta-analysis
Any data frame apa_flextable and helpers Building tables by hand

Here are a few more. A descriptive statistics table, with a column for each group:

apa_descriptives(d_sat, by = Gender)

Variable

Men (n = 245)

Women (n = 442)

Total (N = 687)

Age, M (SD)

25.92 (9.76)

25.49 (9.34)

25.64 (9.49)

ACT, M (SD)

28.82 (5.05)

28.40 (4.70)

28.55 (4.83)

Verbal, M (SD)

615.36 (114.33)

610.66 (112.81)

612.33 (113.29)

Quantitative, M (SD)

635.87 (116.02)

596.00 (113.07)

610.22 (115.64)

A correlation matrix, with means and standard deviations:

apa_cor(d_sat[, c("Age", "ACT", "Verbal", "Quantitative")])

Variable

M

SD

1

2

3

4

1. 

Age

 25.64

  9.49

—

2. 

ACT

 28.55

  4.83

.11

**

—

3. 

Verbal

612.33

113.29

−.04

.56

***

—

4. 

Quantitative

610.22

115.64

−.03

.59

***

.64

***

—

A list of nested models gives one table, with R2 and its change in rows below each model:

apa_parameters(list(
  lm(ACT ~ Verbal, data = d_sat),
  lm(ACT ~ Verbal + Quantitative, data = d_sat)
))

Model

Variable

B

SE

β

t

df

p

Model 1

Constant

 13.92   

0.84

.00

16.56

685

<.001

Verbal

  0.02   

0.00

.56

17.71

685

<.001

R2

   .31   

F(1, 685)

313.75***

Model 2

Constant

 10.57   

0.85

.00

12.37

684

<.001

Verbal

  0.01   

0.00

.31

 8.05

684

<.001

Quantitative

  0.02   

0.00

.39

 9.99

684

<.001

R2

   .40   

ΔR2

   .09   

F(1, 684)

 99.79***

APA Style Outside of Tables

apa7’s formatting functions are useful in text, too, especially in inline R code (e.g., `r apa_p(.0004, inline = TRUE)`).

p-values have no leading zero, and very small ones are written as “<.001” in tables (and “p < .001” in text):

apa_p(c(.0004, .0312, .5))
#> [1] "<.001" ".03"   ".50"
apa_p(.0312, inline = TRUE)
#> [1] "*p* = .03"

p2stars converts p-values to stars, and apa_p_star_note writes the note that explains them:

p2stars(c(.0004, .0312, .5))
#> [1] "\\*\\*\\*" "\\*"       ""
apa_p_star_note()
#> [1] "^\\* ^*p* ≤ .05. ^\\*\\* ^*p* ≤ .01. ^\\*\\*\\* ^*p* ≤ .001"

align_chr rounds numbers, replaces hyphens with true minus signs, and pads the numbers so that they line up on their decimals in a table column. sci_md writes very large or very small numbers in scientific notation:

align_chr(c(2.431, -0.4, -10))
#> [1] "  2.43" " −0.40" "−10.00"
sci_md(c(123251, 0.00045, 12.3))
#> [1] "1.23×10^5^"  "4.50×10^−4^" "12.30"

The markdown helpers write italics, bold, subscripts, and superscripts, which apa7 tables and Quarto documents display:

italic_md("M")
#> [1] "*M*"
paste0("*R*", superscript_md("2"), subscript_md("adj"))
#> [1] "*R*^2^~adj~"

Defaults

apa7_defaults sets options for all tables, such as the rounding accuracy, the font and its size, the line spacing, the label of the intercept, and the width of the page that tables fill (6.5 inches, the text width of letter-size paper with 1-inch margins, unless changed to another width in inches or to a paper size such as "a4"):

apa7_defaults(accuracy = .001, 
              font_family = "Arial",
              font_size = 11,
              line_spacing = 1,
              intercept_text = "Intercept",
              width_aware_padding = FALSE,
              page_width = "a4", 
              print = "tibble",
              citations = "keys"
              )

# Back to the defaults
apa7_defaults(reset = TRUE)

Each of these settings is also an R option

Argument Option Default
accuracy apa7.accuracy .01
font_family apa7.font_family “Times New Roman”
font_size apa7.font_size 12
line_spacing apa7.line_spacing 2
intercept_text apa7.intercept_text “Constant”
width_aware_padding apa7.width_aware_padding TRUE
page_width apa7.page_width 6.5
landscape_width apa7.landscape_width NA
column_width apa7.column_width NA
table_width apa7.table_width 1
print apa7.print “auto”
citations apa7.citations “auto”

For example, to set the default font family:

options(apa7.font_family = "Arial")

Table Widths

A manuscript can have tables of three widths: the text area of a portrait page, the text area of a landscape page, and one column of a two-column page. Every apa7 function that makes a table has a table_width argument that can name any of them:

apa_flextable(apa_parameters(fit), table_width = "page")      # 6.5 in (the default)
apa_flextable(apa_parameters(fit), table_width = "landscape") # 9 in
apa_flextable(apa_parameters(fit), table_width = "column")    # 3.36 in

The widths in the comments are for letter-size paper. They follow the page_width setting: with apa7_defaults(page_width = "a4"), they are 6.27, 9.69, and 3.25 inches. A landscape page’s text width is the paper’s height less 1-inch margins, and a column’s width is that of a column in apaquarto’s two-column journal mode (documentmode: jou), which has 0.75-inch side margins and a gutter of 4% of the text width between the columns. To set either one yourself, use the landscape_width and column_width arguments of apa7_defaults (in inches).

A table can also be a proportion of one of these widths, or have a width in inches, centimeters, or millimeters:

table_width Width
1 The default width (the portrait page, unless changed)
.5 Half the default width
"page" or "portrait" The text area of a portrait page
"landscape" The text area of a landscape page
"column" One column of a two-column page
c(landscape = .8) 80% of the landscape width
c(cm = 10) 10 centimeters (also inches and mm)
0 Only as wide as the contents

One Table at a Time

In a Quarto document, the width of one table can be set in its code chunk with the apa7-width option, so that a table printed automatically (without apa_flextable) gets that width, and no other table is affected:

```{r}
#| label: tbl-landscape
#| apa7-width: landscape
apa_parameters(fit)
```

A named number is written as a YAML map (e.g., #| apa7-width: {landscape: .8}). In R Markdown, the option is apa7.width (e.g., {r, apa7.width = "landscape"}). A table_width given to a function overrides the chunk option.

Widths From the Document

You may not need to set any widths at all. When page_width and table_width are not set with apa7_defaults, apa7 reads them from the document being rendered: its papersize (e.g., papersize: a4) sets the paper, and apaquarto’s documentmode sets the margins. In journal mode (documentmode: jou), which sets the text in two columns with 0.75-inch margins, every table is as wide as a column unless it asks for another width, and a table in a chunk with apa-twocolumn: true spans both columns.

Two-Column Documents

In a two-column document that is not in apaquarto’s journal mode, make the column the default width for every table:

apa7_defaults(table_width = "column")

Proportions are then proportions of the column (e.g., table_width = .5 is half a column). A table that spans both columns, which apaquarto makes with the apa-twocolumn: true chunk option, is as wide as the page without any other setting.

Writing a Paper with apaquarto

apaquarto is a Quarto extension that formats a whole document in APA style, with the title page, headings, citations, references, and figures and tables in their proper places, and renders it to Word, HTML, PDF, or Typst. install_apaquarto installs it in the current project, and make_apaquarto opens an app that writes a new document for you: