View source: R/apa_corrTable.R
apa_corrTable | R Documentation |
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
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, ... )
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. |
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 |
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 |
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 |
... |
(Optional), Additional arguments that can be passed to |
A flextable
object with APA ready correlation table.
Bjoern Buedenbender (Formatting based on Remi Theriault, Correlations based on Dominik Vogel)
get_number_of_decimals
, format_flextable
,
serialNext
library(datscience) apa_corrTable(mtcars, table_caption = c("Table 2", "Correlations in the mtcars Data Set"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.