Reshape correlation analysis results. Key functions:

  • cor_gather(): takes a correlation matrix and collapses (i.e. melt) it into a paired list (long format).

  • cor_spread(): spread a long correlation data format across multiple columns. Particularly, it takes the results of cor_test and transforms it into a correlation matrix.

cor_gather(data, drop.na = TRUE)

cor_spread(data, value = "cor")

Arguments

data

a data frame or matrix.

drop.na

logical. If TRUE, drop rows containing missing values after gathering the data.

value

column name containing the value to spread.

Functions

  • cor_gather(): takes a correlation matrix and collapses (or melt) it into long format data frame (paired list)

  • cor_spread(): spread a long correlation data frame into wide format. Expects the columns "var1", "var2" and "cor" in the data. (correlation matrix).

See also

Examples

# Data preparation
#::::::::::::::::::::::::::::::::::::::::::
mydata <- mtcars %>%
  select(mpg, disp, hp, drat, wt, qsec)
head(mydata, 3)
#>                mpg disp  hp drat    wt  qsec
#> Mazda RX4     21.0  160 110 3.90 2.620 16.46
#> Mazda RX4 Wag 21.0  160 110 3.90 2.875 17.02
#> Datsun 710    22.8  108  93 3.85 2.320 18.61

# Reshape a correlation matrix
#::::::::::::::::::::::::::::::::::::::::::
# Compute a correlation matrix
cor.mat <- mydata %>% cor_mat()
cor.mat
#> # A tibble: 6 × 7
#>   rowname   mpg  disp    hp   drat    wt   qsec
#> * <chr>   <dbl> <dbl> <dbl>  <dbl> <dbl>  <dbl>
#> 1 mpg      1    -0.85 -0.78  0.68  -0.87  0.42 
#> 2 disp    -0.85  1     0.79 -0.71   0.89 -0.43 
#> 3 hp      -0.78  0.79  1    -0.45   0.66 -0.71 
#> 4 drat     0.68 -0.71 -0.45  1     -0.71  0.091
#> 5 wt      -0.87  0.89  0.66 -0.71   1    -0.17 
#> 6 qsec     0.42 -0.43 -0.71  0.091 -0.17  1    

# Collapse the correlation matrix into long format
# paired list data frame
long.format <- cor.mat %>% cor_gather()
long.format
#> # A tibble: 36 × 4
#>    var1  var2    cor        p
#>    <chr> <chr> <dbl>    <dbl>
#>  1 mpg   mpg    1    0       
#>  2 disp  mpg   -0.85 9.38e-10
#>  3 hp    mpg   -0.78 1.79e- 7
#>  4 drat  mpg    0.68 1.78e- 5
#>  5 wt    mpg   -0.87 1.29e-10
#>  6 qsec  mpg    0.42 1.71e- 2
#>  7 mpg   disp  -0.85 9.38e-10
#>  8 disp  disp   1    0       
#>  9 hp    disp   0.79 7.14e- 8
#> 10 drat  disp  -0.71 5.28e- 6
#> # … with 26 more rows

# Spread a correlation data format
#::::::::::::::::::::::::::::::::::::::::::
# Spread the correlation coefficient value
long.format %>% cor_spread(value = "cor")
#> # A tibble: 6 × 7
#>   rowname   mpg  disp    hp   drat    wt   qsec
#>   <chr>   <dbl> <dbl> <dbl>  <dbl> <dbl>  <dbl>
#> 1 mpg      1    -0.85 -0.78  0.68  -0.87  0.42 
#> 2 disp    -0.85  1     0.79 -0.71   0.89 -0.43 
#> 3 hp      -0.78  0.79  1    -0.45   0.66 -0.71 
#> 4 drat     0.68 -0.71 -0.45  1     -0.71  0.091
#> 5 wt      -0.87  0.89  0.66 -0.71   1    -0.17 
#> 6 qsec     0.42 -0.43 -0.71  0.091 -0.17  1    
# Spread the p-value
long.format %>% cor_spread(value = "p")
#> # A tibble: 6 × 7
#>   rowname      mpg     disp           hp       drat        wt       qsec
#>   <chr>      <dbl>    <dbl>        <dbl>      <dbl>     <dbl>      <dbl>
#> 1 mpg     0        9.38e-10 0.000000179  0.0000178  1.29e- 10 0.0171    
#> 2 disp    9.38e-10 0        0.0000000714 0.00000528 1.22e- 11 0.0131    
#> 3 hp      1.79e- 7 7.14e- 8 0            0.00999    4.15e-  5 0.00000577
#> 4 drat    1.78e- 5 5.28e- 6 0.00999      0          4.78e-  6 0.62      
#> 5 wt      1.29e-10 1.22e-11 0.0000415    0.00000478 2.27e-236 0.339     
#> 6 qsec    1.71e- 2 1.31e- 2 0.00000577   0.62       3.39e-  1 0