Skip to contents
library(apa7)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

Some tables, like correlation matrices, can be standardized and automated. Others are uniquely tied to a specific purpose, and no function could anticipate their structure. The apa7 package has a few functions to automate what can be automated and a styling function to make the final styling of a complex table a little easier.

By default, the output of apa_cor flextable, with significant correlations bolded.

trees |> 
  apa_cor()
#> Registered S3 method overwritten by 'ftExtra':
#>   method                  from     
#>   as_flextable.data.frame flextable

Variable

M

SD

1

2

3

1. Girth

13.25

3.14

2. Height

76.00

6.37

.52

3. Volume

30.17

16.44

.97

.60

Note. Correlations signficant at p < .05 are bolded.

If you prefer to work with gt tables, set output function to gt.

trees |> 
  apa_cor(output = "gt") 
Variable M SD 1 2 3
1. Girth 13.25 3.14

2. Height 76.00 6.37 .52
3. Volume 30.17 16.44 .97 .60
Note. Correlations significant at p < .05 are bolded.

If you want to use some other table package, you can output a tibble instead, and then process the table however you want.

options(knitr.kable.NA = '')
trees |> 
  apa_cor(output = "tibble") |> 
  knitr::kable(align = "lrrrrr")
Variable M SD 1 2 3
1. Girth 13.25 3.14
2. Height 76.00 6.37 .52
3. Volume 30.17 16.44 .97 .60

Styling options

Your styling options include:

  • note A custom markdown formatted note. Overrides automatic note about bolded correlations being significant. Default: NULL
  • p_value Significance value. Default: .05
  • digits Number of digits for rounding. Default: 2
  • bold_significant Whether to bold significant correlations. Default: TRUE
  • significance_note Whether note about bolding correlations appears. Overridden by a custom note. Also, the automatic note will not appear if no correlations are significant. Default: TRUE
  • output Options are flextable, gt or tibble. Default: flextable
  • family Font family (typeface). Default: Times New Roman
  • font_size Font size (in points). Default: 12
  • text_color Text color. Default: black
  • border_color Table border color. Default: black
  • border_width Table border width. Default: 0.5
  • line_spacing Text line spacing. Default: 2

For example,

# List of descriptive functions
my_summaries <- list(M = mean,
                     SD = sd,
                     Skew = psych::skew,
                     `Kurt.` = psych::kurtosi,
                     n = function(x) sum(!is.na(x)))
trees |> 
  apa_cor(note = "*M* = Mean, *SD* = Standard Deviation, *Skew* =  Skewness, *Kurt.* =  Kurtosis",
          p_value = .001, 
          digits = 2,
          bold_significant = FALSE, 
          significance_note = FALSE,
          summary_functions = my_summaries,
          output = "gt", 
          family = "Arial", 
          font_size = 10, 
          text_color = "darkred",
          border_color = "royalblue4", 
          border_width = 1, 
          line_spacing = 2)
Variable M SD Skew Kurt. n 1 2 3
1. Girth 13.25 3.14 0.50 −0.71 31

2. Height 76.00 6.37 −0.36 −0.72 31 .52
3. Volume 30.17 16.44 1.01 0.25 31 .97 .60
Note. M = Mean, SD = Standard Deviation, Skew = Skewness, Kurt. = Kurtosis

To remove summary statistics entirely, set summary_functions to NULL.

x <- apa_cor(trees, 
        summary_functions = NULL, 
        bold_significant = FALSE, output = "gt") 
  

Customization

The styling options for apa_cor are minimal, but flexable and gt tables allow for all kinds of customization. Just place whatever code from flextable or gt you want after apa_cor. For example, gt tables usually span 100% of the width in a space. Here I set the width to 50% and align the table to the left of the page. I place a footnote in a specific cell in the table body.

apa_cor(trees, output = "gt", significance_note = F) |> 
  gt::tab_options(table.width = gt::pct(50), 
                  table.align = "left") |> 
  gt::tab_footnote("This is a footnote.", 
                   locations = gt::cells_body(columns = "Variable", 
                                              rows = 2))
Variable M SD 1 2 3
1. Girth 13.25 3.14

2. Height1 76.00 6.37 .52
3. Volume 30.17 16.44 .97 .60
1 This is a footnote.

Here I do the same with flextable:

apa_cor(trees, significance_note = F) |>
  flextable::set_table_properties(align = "left") |>
  flextable::width(j = c("Variable", "M", "SD", "1", "2", "3"), width = c(1, .45, .45, .45, .45, .45)) |> 
  flextable::footnote(
    i = 2,
    j = "Variable",
    value = flextable::as_paragraph("This is a footnote."),
    part = "body"
  ) 

Variable

M

SD

1

2

3

1. Girth

13.25

3.14

2. Height1

76.00

6.37

.52

3. Volume

30.17

16.44

.97

.60

1This is a footnote.