Create an APA style correlation table with R
We will create the correlation matrix using Hmisc::rcorr
to get the correlations along with the p-values and knitr::kable
to print the table.
Using R-Studio, we can knit directly to word and format it the way we like.
The general format for each of the cell is to display the correlation, along with stars to indicate the significance level.
We want to apply a function (Pasting *
) for each cell that is below a specific level.
Helper Function
First we define a function that allows us to apply a function to each cell that satisfies a property. There may already be a function for this, but I could not find it.
APA Correlation Matrix
Now we can define a function to return the matrix with the stars appended to significant correlations. The row names are kept with the index appended. The column names are replaced with the index.
Example
Now we can use apaCorr
and kable
to pretty print the correlation table in a format similar to APA.
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | |
---|---|---|---|---|---|---|---|---|---|---|---|
1. Mpg | - | ||||||||||
2. Cyl | -0.85*** | - | |||||||||
3. Disp | -0.85*** | 0.9*** | - | ||||||||
4. Hp | -0.78*** | 0.83*** | 0.79*** | - | |||||||
5. Drat | 0.68*** | -0.7*** | -0.71*** | -0.45** | - | ||||||
6. Wt | -0.87*** | 0.78*** | 0.89*** | 0.66*** | -0.71*** | - | |||||
7. Qsec | 0.42* | -0.59*** | -0.43* | -0.71*** | 0.09 | -0.17 | - | ||||
8. Vs | 0.66*** | -0.81*** | -0.71*** | -0.72*** | 0.44* | -0.55*** | 0.74*** | - | |||
9. Am | 0.6*** | -0.52** | -0.59*** | -0.24 | 0.71*** | -0.69*** | -0.23 | 0.17 | - | ||
10. Gear | 0.48** | -0.49** | -0.56*** | -0.13 | 0.7*** | -0.58*** | -0.21 | 0.21 | 0.79*** | - | |
11. Carb | -0.55** | 0.53** | 0.39* | 0.75*** | -0.09 | 0.43* | -0.66*** | -0.57*** | 0.06 | 0.27 | - |