Skip to contents

The Idea was inspired by a blog post of my colleague Remi Theriault (see remi-theriault.com) Which utilized the ability of flextable to be able to get an APA-style formatted table directly from R into .docx (word). One frequent use case is to get a correlation table into word. For the creation of the correlation table this function uses the corstars function from this package, which resolves around the code by Dominik Vogel.
I built on those idea and created a function that creates a pretty correlation table together with the summary stats of your choice, and returns a flextable, which can be easily exported to word (see also save_flextable)
Please note:

  • This function only considers numeric variables (cols). Other datatypes will be dropped from the data.frame df.

  • There is an interaction between the arguments sig.level and nod. When sig.level is set to NA, this improves the formatting of decimals places in the finale table, and removes the stars marking significance. However, this also prevents the table from having differing number of decimal places. Meaning the number of decimals for the correlation will also determine the number of decimals for the summary statistics

Usage

apa_corrTable(
  df,
  summarystats = c("mean", "sd"),
  method = c("pearson", "spearman", "polychoric"),
  rmDiag = FALSE,
  sig.level = 0.05,
  nod = c(2, -1),
  filepath = NA,
  overwrite = FALSE,
  ...
)

Arguments

df

Data.frame, mandatory argument. Consider filtering before passing it e.g. with dplyr::select() and dplyr::filter()

summarystats

A vector with the summary stats to be included at the bottom below the correlation.
Default is c("mean","sd") Options are one or all of c("mean","sd","median","range","min","max","skew", "kurtosis","se","missing"). The option "missing", adds missings per item as additional row (accecpts both "missing" and "missings", spelling in table accordingly). If NA is given, no summarystats will be added.

method

Type of correlation. Options are currently: "pearson", "spearman" and "polychoric"

rmDiag

Should the diagonal in the corr matrix kept (FALSE) or removed (TRUE)

sig.level

How many stars per level of significance, options include .05 .01 or .001. If NA no stars indicating significance will be output. This improves formatting of decimals in the table. Note the default for polychoric is NA

nod

(Optional) Integer or Integer Vector. Number of Decimals. In case of -1 a simple convention based on sample size is applied for determination of number of decimal points. See get_number_of_decimals. You can also provide an Integer vector, if you want different number of decimals for the correlations and the summary stats. The first integer determines nod for correlations, the second for summary stats. E.g., nod = c(2,-1) would give 2 decimals for correlations and apply the convention for summary stats. Default is nod = c(2,-1).
See get_number_of_decimals or?datscience::get_number_of_decimals

filepath

(Optional) Path and filename were the APA ready table should be saved, options include the common filetypes .docx (Word), .pptx (Powerpoint), .html (Webpage). Default is filepath = NA. If NA is given, no file will be saved.

overwrite

(Optional) Boolean, default is FALSE. When overwrite is FALSE and the files already exists, a serialized version of the filename will be created (i.e., appending _001, or _002). Utilizes serialNext

...

(Optional), Additional arguments that can be passed to format_flextable (e.g., fontsize, font ...) or to serialNext

Value

A flextable object with APA ready correlation table.

Author

Bjoern Buedenbender (Formatting based on Remi Theriault, Correlations based on Dominik Vogel)

Examples

library(datscience)
apa_corrTable(mtcars, table_caption = c("Table 2", "Correlations in the mtcars Data Set"))
#> a flextable object.
#> col_keys: ` `, `mpg`, `cyl`, `disp`, `hp`, `drat`, `wt`, `qsec`, `vs`, `am`, `gear`, `carb` 
#> header has 3 row(s) 
#> body has 13 row(s) 
#> original dataset sample: 
#>              mpg       cyl      disp        hp   drat wt qsec vs am gear carb
#> 1  mpg     1.00                                                              
#> 2  cyl -0.85***      1.00                                                    
#> 3 disp -0.85***   0.90***      1.00                                          
#> 4   hp -0.78***   0.83***   0.79***      1.00                                
#> 5 drat  0.68***  -0.70***  -0.71***  -0.45**    1.00