You can use the correlation package for this.
The correlation() function computes correlations among the specified variables and returns a data frame with correlations, confidence intervals, and test values for each pair of variables:
library(correlation)
my_data <- mtcars[, c(1,3,4,5,6,7)]
(corr <- correlation(my_data))
#> # Correlation Matrix (pearson-method)
#>
#> Parameter1 | Parameter2 | r | 95% CI | t(30) | p
#> --------------------------------------------------------------------
#> mpg | disp | -0.85 | [-0.92, -0.71] | -8.75 | < .001***
#> mpg | hp | -0.78 | [-0.89, -0.59] | -6.74 | < .001***
#> mpg | drat | 0.68 | [ 0.44, 0.83] | 5.10 | < .001***
#> mpg | wt | -0.87 | [-0.93, -0.74] | -9.56 | < .001***
#> mpg | qsec | 0.42 | [ 0.08, 0.67] | 2.53 | 0.053
#> disp | hp | 0.79 | [ 0.61, 0.89] | 7.08 | < .001***
#> disp | drat | -0.71 | [-0.85, -0.48] | -5.53 | < .001***
#> disp | wt | 0.89 | [ 0.78, 0.94] | 10.58 | < .001***
#> disp | qsec | -0.43 | [-0.68, -0.10] | -2.64 | 0.053
#> hp | drat | -0.45 | [-0.69, -0.12] | -2.75 | < .05*
#> hp | wt | 0.66 | [ 0.40, 0.82] | 4.80 | < .001***
#> hp | qsec | -0.71 | [-0.85, -0.48] | -5.49 | < .001***
#> drat | wt | -0.71 | [-0.85, -0.48] | -5.56 | < .001***
#> drat | qsec | 0.09 | [-0.27, 0.43] | 0.50 | 0.678
#> wt | qsec | -0.17 | [-0.49, 0.19] | -0.97 | 0.678
#>
#> p-value adjustment method: Holm (1979)
#> Observations: 32
If you pass this correlation object to summary(), it will return a compact matrix with just the correlations and p value stars.
(corrmat <- summary(corr))
#> # Correlation Matrix (pearson-method)
#>
#> Parameter | qsec | wt | drat | hp | disp
#> ----------------------------------------------------------------
#> mpg | 0.42 | -0.87*** | 0.68*** | -0.78*** | -0.85***
#> disp | -0.43 | 0.89*** | -0.71*** | 0.79*** |
#> hp | -0.71*** | 0.66*** | -0.45* | |
#> drat | 0.09 | -0.71*** | | |
#> wt | -0.17 | | | |
#>
#> p-value adjustment method: Holm (1979)
If you are writing this script in Markdown, you can get a markdown-formatted table with the display() function.
# For rendering in markdown:
display(corrmat)
| Parameter |
qsec |
wt |
drat |
hp |
disp |
| mpg |
0.42 |
-0.87*** |
0.68*** |
-0.78*** |
-0.85*** |
| disp |
-0.43 |
0.89*** |
-0.71*** |
0.79*** |
|
| hp |
-0.71*** |
0.66*** |
-0.45* |
|
|
| drat |
0.09 |
-0.71*** |
|
|
|
| wt |
-0.17 |
|
|
|
|
Correlation Matrix (pearson-method)
p-value adjustment method: Holm (1979)
Personally, I would recommend prioritizing the confidence intervals over the p values or significance stars, as they are much more informative. You can get a table with correlations and confidence intervals with the apaTables package.
apaTables::apa.cor.table(my_data)
#>
#>
#> Means, standard deviations, and correlations with confidence intervals
#>
#>
#> Variable M SD 1 2 3 4 5
#> 1. mpg 20.09 6.03
#>
#> 2. disp 230.72 123.94 -.85**
#> [-.92, -.71]
#>
#> 3. hp 146.69 68.56 -.78** .79**
#> [-.89, -.59] [.61, .89]
#>
#> 4. drat 3.60 0.53 .68** -.71** -.45**
#> [.44, .83] [-.85, -.48] [-.69, -.12]
#>
#> 5. wt 3.22 0.98 -.87** .89** .66** -.71**
#> [-.93, -.74] [.78, .94] [.40, .82] [-.85, -.48]
#>
#> 6. qsec 17.85 1.79 .42* -.43* -.71** .09 -.17
#> [.08, .67] [-.68, -.10] [-.85, -.48] [-.27, .43] [-.49, .19]
#> Note. M and SD are used to represent mean and standard deviation, respectively.
#> Values in square brackets indicate the 95% confidence interval.
#> The confidence interval is a plausible range of population correlations
#> that could have caused the sample correlation (Cumming, 2014).
#> * indicates p < .05. ** indicates p < .01.
#>
Created on 2021-05-30 by the reprex package (v2.0.0)