This function turns the correlation matrix returned by cor_matrix()
and
its relatives into a tidy dataframe. Note that by default, results for both the cor(A, B)
and cor(B, A)
are returned, while entries for A, A
, i.e. the values on the diagonal,
are never included.
# S3 method for class 'cor_matrix'
tidy(x, both_directions = TRUE, ...)
A cor_marix
object returned from
cor_matrix()
Should both cor(A, B)
and cor(B, A)
be returned. Defaults to TRUE
.
Additional arguments. Not used. Needed to match generic signature only.
A tibble::tibble()
with columns:
Name of the first variable
Name of the second variable
The estimated value of the correlation
The t-statistic used for significance testing
The two-sided p-value of the correlation
Number of observations used to compute the correlation
Lower bound of confidence interval. Width is determined in call to cor_matrix()
Upper bound of confidence interval. Width is determined in call to cor_matrix()
# Create and tidy a correlation matrix
cm <- cor_matrix(iris)
tidy(cm, both_directions = FALSE)
#> # A tibble: 6 × 9
#> column1 column2 estimate std.error p.value statistic n conf.low conf.high
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 Sepal.… Sepal.… -0.118 0.0816 1.52e- 1 -1.44 150 -0.273 0.0435
#> 2 Petal.… Sepal.… 0.872 0.0403 1.04e-47 21.6 150 0.827 0.906
#> 3 Petal.… Sepal.… 0.818 0.0473 2.33e-37 17.3 150 0.757 0.865
#> 4 Petal.… Sepal.… -0.428 0.0743 4.51e- 8 -5.77 150 -0.551 -0.288
#> 5 Petal.… Sepal.… -0.366 0.0765 4.07e- 6 -4.79 150 -0.497 -0.219
#> 6 Petal.… Petal.… 0.963 0.0222 4.68e-86 43.4 150 0.949 0.973
# With ESS health data
cm_ess <- cor_matrix(ess_health,
var_names = c(health = "Health", dosprt = "Sport"))
tidy(cm_ess)
#> # A tibble: 2 × 9
#> column2 column1 estimate std.error p.value statistic n conf.low conf.high
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 Sport Health -0.183 0.0116 5.86e-55 -15.7 7197 -0.205 -0.160
#> 2 Health Sport -0.183 0.0116 5.86e-55 -15.7 7197 -0.205 -0.160