<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/focus-on-correlations-of-some-variables-with-many-others/"></div></div><div><a data-url="https://www.r-bloggers.com/focus-on-correlations-of-some-variables-with-many-others/" data-counturl="https://www.r-bloggers.com/focus-on-correlations-of-some-variables-with-many-others/" data-count="horizontal" data-text="focus() on correlations of some variables with many others" 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/how-does-one-variable-correlate-with-all-others"> blogR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Get the correlations of one or more variables with many others using <code>focus()</code> from the corrr package:</p>
<pre><code>library(corrr)
mtcars %>% correlate() %>% focus(mpg)
#> # A tibble: 10 x 2
#>    rowname        mpg
#>      &lt;chr>      &lt;dbl>
#> 1      cyl -0.8521620
#> 2     disp -0.8475514
#> 3       hp -0.7761684
#> 4     drat  0.6811719
#> 5       wt -0.8676594
#> 6     qsec  0.4186840
#> 7       vs  0.6640389
#> 8       am  0.5998324
#> 9     gear  0.4802848
#> 10    carb -0.5509251
</code></pre>
<p>Let’s break it down.</p>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/how-does-one-variable-correlate-with-all-others#motivation_2"> </a>Motivation</h2>
<p>I’ve noticed a lot of people asking how to do this: see <a target="_blank" href="http://stackoverflow.com/questions/38660129/how-to-correlate-and-visualise-correlation-of-one-variable-versus-many/38669144#38669144">here</a>, <a target="_blank" href="http://stackoverflow.com/questions/38548943/correlation-between-multiple-variables-of-a-data-frame/38558925#38558925">here</a>, <a target="_blank" href="http://stackoverflow.com/questions/38331537/r-using-cor-test-on-multiple-columns-on-a-data-frame/38559334#38559334">here</a>.</p>
<p>So this post will explain how to use <code>focus()</code> from the corrr package to correlate one or more variables in a data frame with many others.</p>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/how-does-one-variable-correlate-with-all-others#starting-with-corrr_2"> </a>Starting with corrr</h2>
<p>We’ll be using the <a target="_blank" href="https://cran.rstudio.com/web/packages/corrr/index.html">corrr</a> package, which starts by using <code>correlate()</code> to create a correlation data frame. For example, we can <code>correlate()</code> all columns in the <code>mtcars</code> data set as follows:</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>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/how-does-one-variable-correlate-with-all-others#introducing-code-classprettyprintfocuscode_2"> </a>Introducing <code>focus()</code></h2>
<p>Once we have a correlation data frame, we can <code>focus()</code> on subsections of these results. For example, to <code>focus()</code> on the correlations of the <code>mpg</code> variable with all other variables, we do:</p>
<pre><code>mtcars %>% correlate() %>% focus(mpg)
#> # A tibble: 10 x 2
#>    rowname        mpg
#>      &lt;chr>      &lt;dbl>
#> 1      cyl -0.8521620
#> 2     disp -0.8475514
#> 3       hp -0.7761684
#> 4     drat  0.6811719
#> 5       wt -0.8676594
#> 6     qsec  0.4186840
#> 7       vs  0.6640389
#> 8       am  0.5998324
#> 9     gear  0.4802848
#> 10    carb -0.5509251
</code></pre>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/how-does-one-variable-correlate-with-all-others#how-does-it-work_2"> </a>How does it work?</h2>
<p><code>focus()</code> works similarly to <code>select()</code> from the <a target="_blank" href="https://cran.r-project.org/web/packages/dplyr/index.html">dplyr</a> package (which is loaded along with the corrr package). You add the names of the columns you wish to keep in your correlation data frame. Extending <code>select()</code>, <code>focus()</code> will then remove the remaining column variables from the rows. This is why <code>mpg</code> does not appear in the rows above. Here’s another example with two variables:</p>
<pre><code>mtcars %>% correlate() %>% focus(mpg, disp)
#> # A tibble: 9 x 3
#>   rowname        mpg       disp
#>     &lt;chr>      &lt;dbl>      &lt;dbl>
#> 1     cyl -0.8521620  0.9020329
#> 2      hp -0.7761684  0.7909486
#> 3    drat  0.6811719 -0.7102139
#> 4      wt -0.8676594  0.8879799
#> 5    qsec  0.4186840 -0.4336979
#> 6      vs  0.6640389 -0.7104159
#> 7      am  0.5998324 -0.5912270
#> 8    gear  0.4802848 -0.5555692
#> 9    carb -0.5509251  0.3949769
</code></pre>
<p><code>focus()</code> gives us all the same syntax as <code>select()</code>. For example, to drop a column (instead of keeping it), we use <code>-</code>, which will then keep it in the rows:</p>
<pre><code>mtcars %>% correlate() %>% focus(-mpg)
#> # A tibble: 1 x 11
#>   rowname       cyl       disp         hp      drat         wt     qsec
#>     &lt;chr>     &lt;dbl>      &lt;dbl>      &lt;dbl>     &lt;dbl>      &lt;dbl>    &lt;dbl>
#> 1     mpg -0.852162 -0.8475514 -0.7761684 0.6811719 -0.8676594 0.418684
#> # ... with 4 more variables: vs &lt;dbl>, am &lt;dbl>, gear &lt;dbl>, carb &lt;dbl>
</code></pre>
<p>Or use utility functions like <code>contains()</code>:</p>
<pre><code>iris[-5] %>% correlate() %>% focus(contains(&quot;Sepal&quot;))
#> # A tibble: 2 x 3
#>        rowname Sepal.Length Sepal.Width
#>          &lt;chr>        &lt;dbl>       &lt;dbl>
#> 1 Petal.Length    0.8717538  -0.4284401
#> 2  Petal.Width    0.8179411  -0.3661259
</code></pre>
<p>For the full list of uses, explore <code>?select</code>.</p>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/how-does-one-variable-correlate-with-all-others#working-with-the-results_2"> </a>Working with the results</h2>
<p>Let’s take the case of correlating one variable with all others (e.g., <code>mpg</code> with all others). What should we do? Well, we have a data_frame with two columns:</p>
<ul>
<li> <code>rowname</code> which has the names of the other variables.</li>
<li> <code>mpg</code> which has the correlations of <code>mpg</code> with the other variables.</li>
</ul>
<p>With this in mind, one thing we can do is describe various aspects of the correlations within <code>summarise()</code> (also from dplyr package):</p>
<pre><code>mtcars %>% correlate() %>% focus(mpg) %>% summarise(
  n    = n(),
  mean = mean(mpg),
  sd   = sd(mpg),
  min  = min(mpg),
  max  = max(mpg)
)
#> # A tibble: 1 x 5
#>       n       mean        sd        min       max
#>   &lt;int>      &lt;dbl>     &lt;dbl>      &lt;dbl>     &lt;dbl>
#> 1    10 -0.1050454 0.7198517 -0.8676594 0.6811719
</code></pre>
<p>I also like to visualise the correlations. For example, using the <a target="_blank" href="https://cran.r-project.org/web/packages/ggplot2/index.html">ggplot2</a> package:</p>
<pre><code>library(ggplot2)
mtcars %>% correlate() %>% focus(mpg) %>%
  ggplot(aes(x = rowname, y = mpg)) +
    geom_bar(stat = &quot;identity&quot;) +
    ylab(&quot;Correlation with mpg&quot;) +
    xlab(&quot;Variable&quot;)
</code></pre>
<p><a target="_blank" href="https://i2.wp.com/svbtleusercontent.com/6c6654kyyigya.png?ssl=1"><img alt="plot1-1.png" src="https://i0.wp.com/svbtleusercontent.com/6c6654kyyigya_small.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>To add to this, we can order <code>rowname</code> by the correlation size in <code>mutate()</code> below:</p>
<pre><code>mtcars %>% correlate() %>% focus(mpg) %>%
  mutate(rowname = factor(rowname, levels = rowname[order(mpg)])) %>%
  ggplot(aes(x = rowname, y = mpg)) +
    geom_bar(stat = &quot;identity&quot;) +
    ylab(&quot;Correlation with mpg&quot;) +
    xlab(&quot;Variable&quot;)
</code></pre>
<p><a target="_blank" href="https://i2.wp.com/svbtleusercontent.com/fvzme1lfyslm9w.png?ssl=1"><img alt="plot2-1.png" src="https://i1.wp.com/svbtleusercontent.com/fvzme1lfyslm9w_small.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/how-does-one-variable-correlate-with-all-others#extra-features_2"> </a>Extra features</h2>
<p>There are a few additional features to <code>focus()</code> that might help you on occasions.</p>
<p>First, you can keep the remaining columns in the rows (and remove all others) with the <code>mirror = TRUE</code> argument:</p>
<pre><code>mtcars %>% correlate() %>% focus(mpg:drat, mirror = TRUE)
#> # A tibble: 5 x 6
#>   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.6811719
#> 2     cyl -0.8521620         NA  0.9020329  0.8324475 -0.6999381
#> 3    disp -0.8475514  0.9020329         NA  0.7909486 -0.7102139
#> 4      hp -0.7761684  0.8324475  0.7909486         NA -0.4487591
#> 5    drat  0.6811719 -0.6999381 -0.7102139 -0.4487591         NA
</code></pre>
<p>For programmers, there’s also a standard evaluation version <code>focus_()</code>, which behaves like <code>select_()</code>.</p>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/how-does-one-variable-correlate-with-all-others#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/focus-on-correlations-of-some-variables-with-many-others/"></div></div><div><a data-url="https://www.r-bloggers.com/focus-on-correlations-of-some-variables-with-many-others/" data-counturl="https://www.r-bloggers.com/focus-on-correlations-of-some-variables-with-many-others/" data-count="vertical" data-text="focus() on correlations of some variables with many others" 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/how-does-one-variable-correlate-with-all-others"> 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/OSxP6IcPS4M"></a></div>