Skip to content

For the purpose of printing, convert a correlation data frame into a noquote matrix with the correlations cleanly formatted (leading zeros removed; spaced for signs) and the diagonal (or any NA) left blank.

Usage

fashion(x, decimals = 2, leading_zeros = FALSE, na_print = "")

Arguments

x

Scalar, vector, matrix or data frame.

decimals

Number of decimal places to display for numbers.

leading_zeros

Should leading zeros be displayed for decimals (e.g., 0.1)? If FALSE, they will be removed.

na_print

Character string indicating NA values in printed output

Value

noquote. Also a data frame if x is a matrix or data frame.

Examples

# Examples with correlate()
library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
mtcars %>% correlate() %>% fashion()
#> Correlation computed with
#>  Method: 'pearson'
#>  Missing treated using: 'pairwise.complete.obs'
#>    term  mpg  cyl disp   hp drat   wt qsec   vs   am gear carb
#> 1   mpg      -.85 -.85 -.78  .68 -.87  .42  .66  .60  .48 -.55
#> 2   cyl -.85       .90  .83 -.70  .78 -.59 -.81 -.52 -.49  .53
#> 3  disp -.85  .90       .79 -.71  .89 -.43 -.71 -.59 -.56  .39
#> 4    hp -.78  .83  .79      -.45  .66 -.71 -.72 -.24 -.13  .75
#> 5  drat  .68 -.70 -.71 -.45      -.71  .09  .44  .71  .70 -.09
#> 6    wt -.87  .78  .89  .66 -.71      -.17 -.55 -.69 -.58  .43
#> 7  qsec  .42 -.59 -.43 -.71  .09 -.17       .74 -.23 -.21 -.66
#> 8    vs  .66 -.81 -.71 -.72  .44 -.55  .74       .17  .21 -.57
#> 9    am  .60 -.52 -.59 -.24  .71 -.69 -.23  .17       .79  .06
#> 10 gear  .48 -.49 -.56 -.13  .70 -.58 -.21  .21  .79       .27
#> 11 carb -.55  .53  .39  .75 -.09  .43 -.66 -.57  .06  .27     
mtcars %>% correlate() %>% fashion(decimals = 1)
#> Correlation computed with
#>  Method: 'pearson'
#>  Missing treated using: 'pairwise.complete.obs'
#>    term mpg cyl disp  hp drat  wt qsec  vs  am gear carb
#> 1   mpg     -.9  -.8 -.8   .7 -.9   .4  .7  .6   .5  -.6
#> 2   cyl -.9       .9  .8  -.7  .8  -.6 -.8 -.5  -.5   .5
#> 3  disp -.8  .9       .8  -.7  .9  -.4 -.7 -.6  -.6   .4
#> 4    hp -.8  .8   .8      -.4  .7  -.7 -.7 -.2  -.1   .7
#> 5  drat  .7 -.7  -.7 -.4      -.7   .1  .4  .7   .7  -.1
#> 6    wt -.9  .8   .9  .7  -.7      -.2 -.6 -.7  -.6   .4
#> 7  qsec  .4 -.6  -.4 -.7   .1 -.2       .7 -.2  -.2  -.7
#> 8    vs  .7 -.8  -.7 -.7   .4 -.6   .7      .2   .2  -.6
#> 9    am  .6 -.5  -.6 -.2   .7 -.7  -.2  .2       .8   .1
#> 10 gear  .5 -.5  -.6 -.1   .7 -.6  -.2  .2  .8        .3
#> 11 carb -.6  .5   .4  .7  -.1  .4  -.7 -.6  .1   .3     
mtcars %>% correlate() %>% fashion(leading_zeros = TRUE)
#> Correlation computed with
#>  Method: 'pearson'
#>  Missing treated using: 'pairwise.complete.obs'
#>    term   mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#> 1   mpg       -0.85 -0.85 -0.78  0.68 -0.87  0.42  0.66  0.60  0.48 -0.55
#> 2   cyl -0.85        0.90  0.83 -0.70  0.78 -0.59 -0.81 -0.52 -0.49  0.53
#> 3  disp -0.85  0.90        0.79 -0.71  0.89 -0.43 -0.71 -0.59 -0.56  0.39
#> 4    hp -0.78  0.83  0.79       -0.45  0.66 -0.71 -0.72 -0.24 -0.13  0.75
#> 5  drat  0.68 -0.70 -0.71 -0.45       -0.71  0.09  0.44  0.71  0.70 -0.09
#> 6    wt -0.87  0.78  0.89  0.66 -0.71       -0.17 -0.55 -0.69 -0.58  0.43
#> 7  qsec  0.42 -0.59 -0.43 -0.71  0.09 -0.17        0.74 -0.23 -0.21 -0.66
#> 8    vs  0.66 -0.81 -0.71 -0.72  0.44 -0.55  0.74        0.17  0.21 -0.57
#> 9    am  0.60 -0.52 -0.59 -0.24  0.71 -0.69 -0.23  0.17        0.79  0.06
#> 10 gear  0.48 -0.49 -0.56 -0.13  0.70 -0.58 -0.21  0.21  0.79        0.27
#> 11 carb -0.55  0.53  0.39  0.75 -0.09  0.43 -0.66 -0.57  0.06  0.27      
mtcars %>% correlate() %>% fashion(na_print = "*")
#> Correlation computed with
#>  Method: 'pearson'
#>  Missing treated using: 'pairwise.complete.obs'
#>    term  mpg  cyl disp   hp drat   wt qsec   vs   am gear carb
#> 1   mpg    * -.85 -.85 -.78  .68 -.87  .42  .66  .60  .48 -.55
#> 2   cyl -.85    *  .90  .83 -.70  .78 -.59 -.81 -.52 -.49  .53
#> 3  disp -.85  .90    *  .79 -.71  .89 -.43 -.71 -.59 -.56  .39
#> 4    hp -.78  .83  .79    * -.45  .66 -.71 -.72 -.24 -.13  .75
#> 5  drat  .68 -.70 -.71 -.45    * -.71  .09  .44  .71  .70 -.09
#> 6    wt -.87  .78  .89  .66 -.71    * -.17 -.55 -.69 -.58  .43
#> 7  qsec  .42 -.59 -.43 -.71  .09 -.17    *  .74 -.23 -.21 -.66
#> 8    vs  .66 -.81 -.71 -.72  .44 -.55  .74    *  .17  .21 -.57
#> 9    am  .60 -.52 -.59 -.24  .71 -.69 -.23  .17    *  .79  .06
#> 10 gear  .48 -.49 -.56 -.13  .70 -.58 -.21  .21  .79    *  .27
#> 11 carb -.55  .53  .39  .75 -.09  .43 -.66 -.57  .06  .27    *

# But doesn't have to include correlate()
mtcars %>% fashion(decimals = 3)
#>       mpg   cyl    disp      hp  drat    wt   qsec    vs    am  gear
#> 1  21.000 6.000 160.000 110.000 3.900 2.620 16.460  .000 1.000 4.000
#> 2  21.000 6.000 160.000 110.000 3.900 2.875 17.020  .000 1.000 4.000
#> 3  22.800 4.000 108.000  93.000 3.850 2.320 18.610 1.000 1.000 4.000
#> 4  21.400 6.000 258.000 110.000 3.080 3.215 19.440 1.000  .000 3.000
#> 5  18.700 8.000 360.000 175.000 3.150 3.440 17.020  .000  .000 3.000
#> 6  18.100 6.000 225.000 105.000 2.760 3.460 20.220 1.000  .000 3.000
#> 7  14.300 8.000 360.000 245.000 3.210 3.570 15.840  .000  .000 3.000
#> 8  24.400 4.000 146.700  62.000 3.690 3.190 20.000 1.000  .000 4.000
#> 9  22.800 4.000 140.800  95.000 3.920 3.150 22.900 1.000  .000 4.000
#> 10 19.200 6.000 167.600 123.000 3.920 3.440 18.300 1.000  .000 4.000
#> 11 17.800 6.000 167.600 123.000 3.920 3.440 18.900 1.000  .000 4.000
#> 12 16.400 8.000 275.800 180.000 3.070 4.070 17.400  .000  .000 3.000
#> 13 17.300 8.000 275.800 180.000 3.070 3.730 17.600  .000  .000 3.000
#> 14 15.200 8.000 275.800 180.000 3.070 3.780 18.000  .000  .000 3.000
#> 15 10.400 8.000 472.000 205.000 2.930 5.250 17.980  .000  .000 3.000
#> 16 10.400 8.000 460.000 215.000 3.000 5.424 17.820  .000  .000 3.000
#> 17 14.700 8.000 440.000 230.000 3.230 5.345 17.420  .000  .000 3.000
#> 18 32.400 4.000  78.700  66.000 4.080 2.200 19.470 1.000 1.000 4.000
#> 19 30.400 4.000  75.700  52.000 4.930 1.615 18.520 1.000 1.000 4.000
#> 20 33.900 4.000  71.100  65.000 4.220 1.835 19.900 1.000 1.000 4.000
#> 21 21.500 4.000 120.100  97.000 3.700 2.465 20.010 1.000  .000 3.000
#> 22 15.500 8.000 318.000 150.000 2.760 3.520 16.870  .000  .000 3.000
#> 23 15.200 8.000 304.000 150.000 3.150 3.435 17.300  .000  .000 3.000
#> 24 13.300 8.000 350.000 245.000 3.730 3.840 15.410  .000  .000 3.000
#> 25 19.200 8.000 400.000 175.000 3.080 3.845 17.050  .000  .000 3.000
#> 26 27.300 4.000  79.000  66.000 4.080 1.935 18.900 1.000 1.000 4.000
#> 27 26.000 4.000 120.300  91.000 4.430 2.140 16.700  .000 1.000 5.000
#> 28 30.400 4.000  95.100 113.000 3.770 1.513 16.900 1.000 1.000 5.000
#> 29 15.800 8.000 351.000 264.000 4.220 3.170 14.500  .000 1.000 5.000
#> 30 19.700 6.000 145.000 175.000 3.620 2.770 15.500  .000 1.000 5.000
#> 31 15.000 8.000 301.000 335.000 3.540 3.570 14.600  .000 1.000 5.000
#> 32 21.400 4.000 121.000 109.000 4.110 2.780 18.600 1.000 1.000 4.000
#>     carb
#> 1  4.000
#> 2  4.000
#> 3  1.000
#> 4  1.000
#> 5  2.000
#> 6  1.000
#> 7  4.000
#> 8  2.000
#> 9  2.000
#> 10 4.000
#> 11 4.000
#> 12 3.000
#> 13 3.000
#> 14 3.000
#> 15 4.000
#> 16 4.000
#> 17 4.000
#> 18 1.000
#> 19 2.000
#> 20 1.000
#> 21 1.000
#> 22 2.000
#> 23 2.000
#> 24 4.000
#> 25 2.000
#> 26 1.000
#> 27 2.000
#> 28 2.000
#> 29 4.000
#> 30 6.000
#> 31 8.000
#> 32 2.000
c(0.234, 134.23, -.23, NA) %>% fashion(na_print = "X")
#> [1]    .23 134.23   -.23 X