<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/fashion-output-with-corrr/"></div></div><div><a data-url="https://www.r-bloggers.com/fashion-output-with-corrr/" data-counturl="https://www.r-bloggers.com/fashion-output-with-corrr/" data-count="horizontal" data-text="fashion() output with corrr" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://drsimonj.svbtle.com/fashion-your-correlations-with-corrr"> blogR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Tired of trying to get your data to print right or formatting it in a program like excel? Try out <code>fashion()</code> from the <code>corrr</code> package:</p>
<pre><code>d &lt;- data.frame(
  gender = factor(c(&quot;Male&quot;, &quot;Female&quot;, NA)),
  age    = c(NA, 28.1111111, 74.3),
  height = c(188, NA, 168.78906),
  fte    = c(NA, .78273, .9)
)
d
#>   gender      age   height     fte
#> 1   Male       NA 188.0000      NA
#> 2 Female 28.11111       NA 0.78273
#> 3   &lt;NA> 74.30000 168.7891 0.90000

library(corrr)
fashion(d)
#>   gender   age height  fte
#> 1   Male       188.00     
#> 2 Female 28.11         .78
#> 3        74.30 168.79  .90
</code></pre>
<p>But how does it work and what does it do?</p>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/fashion-your-correlations-with-corrr#the-inspiration-correlations-and-decimals_2"> </a>The inspiration: correlations and decimals</h2>
<p>The insipration for <code>fashion()</code> came from my unending frustration at getting a correlation matrix to print out exactly how I wanted. For example, printing correlations typically looks something like:</p>
<pre><code>mtcars %>% correlate()
#> # A tibble: 11 x 12
#>    rowname        mpg        cyl       disp         hp        drat
#>      &lt;chr>      &lt;dbl>      &lt;dbl>      &lt;dbl>      &lt;dbl>       &lt;dbl>
#> 1      mpg         NA -0.8521620 -0.8475514 -0.7761684  0.68117191
#> 2      cyl -0.8521620         NA  0.9020329  0.8324475 -0.69993811
#> 3     disp -0.8475514  0.9020329         NA  0.7909486 -0.71021393
#> 4       hp -0.7761684  0.8324475  0.7909486         NA -0.44875912
#> 5     drat  0.6811719 -0.6999381 -0.7102139 -0.4487591          NA
#> 6       wt -0.8676594  0.7824958  0.8879799  0.6587479 -0.71244065
#> 7     qsec  0.4186840 -0.5912421 -0.4336979 -0.7082234  0.09120476
#> 8       vs  0.6640389 -0.8108118 -0.7104159 -0.7230967  0.44027846
#> 9       am  0.5998324 -0.5226070 -0.5912270 -0.2432043  0.71271113
#> 10    gear  0.4802848 -0.4926866 -0.5555692 -0.1257043  0.69961013
#> 11    carb -0.5509251  0.5269883  0.3949769  0.7498125 -0.09078980
#> # ... with 6 more variables: wt &lt;dbl>, qsec &lt;dbl>, vs &lt;dbl>, am &lt;dbl>,
#> #   gear &lt;dbl>, carb &lt;dbl>
</code></pre>
<p>But this is just plain ugly. Personally, I wanted:</p>
<ul>
<li> Decimal places rounded to the same length (usually 2)</li>
<li> All the leading zeros removed, but keeping the decimal aligned with/without <code>-</code> for negative numbers.</li>
<li> Missing values (<code>NA</code>) to appear empty (<code>&quot;&quot;</code>).</li>
</ul>
<p>This is exactly what fashion does:</p>
<pre><code>mtcars %>% correlate() %>% fashion()
#>    rowname  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
</code></pre>
<p>And if I want to change the number of <code>decimal</code> places and have a different place holder for <code>NA</code> values (<code>na_print</code>):</p>
<pre><code>mtcars %>% correlate() %>% fashion(decimals = 1, na_print = &quot;x&quot;)
#>    rowname mpg cyl disp  hp drat  wt qsec  vs  am gear carb
#> 1      mpg   x -.9  -.8 -.8   .7 -.9   .4  .7  .6   .5  -.6
#> 2      cyl -.9   x   .9  .8  -.7  .8  -.6 -.8 -.5  -.5   .5
#> 3     disp -.8  .9    x  .8  -.7  .9  -.4 -.7 -.6  -.6   .4
#> 4       hp -.8  .8   .8   x  -.4  .7  -.7 -.7 -.2  -.1   .7
#> 5     drat  .7 -.7  -.7 -.4    x -.7   .1  .4  .7   .7  -.1
#> 6       wt -.9  .8   .9  .7  -.7   x  -.2 -.6 -.7  -.6   .4
#> 7     qsec  .4 -.6  -.4 -.7   .1 -.2    x  .7 -.2  -.2  -.7
#> 8       vs  .7 -.8  -.7 -.7   .4 -.6   .7   x  .2   .2  -.6
#> 9       am  .6 -.5  -.6 -.2   .7 -.7  -.2  .2   x   .8   .1
#> 10    gear  .5 -.5  -.6 -.1   .7 -.6  -.2  .2  .8    x   .3
#> 11    carb -.6  .5   .4  .7  -.1  .4  -.7 -.6  .1   .3    x
</code></pre>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/fashion-your-correlations-with-corrr#look-but-don39t-touch_2"> </a>Look but don’t touch</h2>
<p>There’s a little bit of magic going on here, but the point to know is that <code>fashion()</code> is returning a noquote version of the original structure:</p>
<pre><code>mtcars %>% correlate() %>% fashion() %>% class()
#> [1] &quot;data.frame&quot; &quot;noquote&quot;
</code></pre>
<p>That means that numbers are no longer numbers.</p>
<pre><code>mtcars %>% correlate() %>% sapply(is.numeric)
#> rowname     mpg     cyl    disp      hp    drat      wt    qsec      vs 
#>   FALSE    TRUE    TRUE    TRUE    TRUE    TRUE    TRUE    TRUE    TRUE 
#>      am    gear    carb 
#>    TRUE    TRUE    TRUE

mtcars %>% correlate() %>% fashion() %>% sapply(is.numeric)
#> rowname     mpg     cyl    disp      hp    drat      wt    qsec      vs 
#>   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE 
#>      am    gear    carb 
#>   FALSE   FALSE   FALSE
</code></pre>
<p>Similarly, missing values are no longer missing values.</p>
<pre><code>mtcars %>% correlate() %>% sapply(function(i) sum(is.na(i)))
#> rowname     mpg     cyl    disp      hp    drat      wt    qsec      vs 
#>       0       1       1       1       1       1       1       1       1 
#>      am    gear    carb 
#>       1       1       1

mtcars %>% correlate() %>% fashion() %>% sapply(function(i) sum(is.na(i)))
#> rowname     mpg     cyl    disp      hp    drat      wt    qsec      vs 
#>       0       0       0       0       0       0       0       0       0 
#>      am    gear    carb 
#>       0       0       0
</code></pre>
<p>So <code>fashion()</code> is for looking at output, not for continuing to work with it.</p>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/fashion-your-correlations-with-corrr#what-to-use-it-on_2"> </a>What to use it on</h2>
<p><code>fashion()</code> can be used on most standard R structures such as scalars, vectors, matrices, data frames, etc:</p>
<pre><code>fashion(10.277)
#> [1] 10.28
fashion(c(10.3785, NA, 87))
#> [1] 10.38       87.00
fashion(matrix(1:4, nrow = 2))
#>     V1   V2
#> 1 1.00 3.00
#> 2 2.00 4.00
</code></pre>
<p>You can also use it on non-numeric data. In this case, all <code>fashion()</code> will do is convert the data to characters, and then alter missing values:</p>
<pre><code>fashion(&quot;Hello&quot;)
#> [1] Hello
fashion(c(&quot;Hello&quot;, NA), na_print = &quot;World&quot;)
#> [1] Hello World
</code></pre>
<p>Now is a good time to take a look back at the opening example to see that it works on a data frame and with a factor column.</p>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/fashion-your-correlations-with-corrr#exporting_2"> </a>Exporting</h2>
<p>Don’t forget that it’s easy to export your fashioned output with something like:</p>
<pre><code>my_data %>% fashion() %>% write.csv(&quot;fashioned_file.csv&quot;)
</code></pre>
<p>So what are you waiting for? Go forth and <code>fashion()</code>!</p>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/fashion-your-correlations-with-corrr#sign-off_2"> </a>Sign off</h2>
<p>Thanks for reading and I hope this was useful for you.</p>
<p>For updates of recent blog posts, follow <a target="_blank" href="https://twitter.com/drsimonj">@drsimonj</a> on Twitter, or email me at <a target="_blank" href="mailto:drsimonjackson@gmail.com">drsimonjackson@gmail.com</a> to get in touch.</p>
<p>If you’d like the code that produced this blog, check out the <a target="_blank" href="https://github.com/drsimonj/blogR">blogR GitHub repository</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/fashion-output-with-corrr/"></div></div><div><a data-url="https://www.r-bloggers.com/fashion-output-with-corrr/" data-counturl="https://www.r-bloggers.com/fashion-output-with-corrr/" data-count="vertical" data-text="fashion() output with corrr" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://drsimonj.svbtle.com/fashion-your-correlations-with-corrr"> blogR</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/bJyMbOa-kbc"></a></div>