<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/the-music-of-les-mills-body-pump-with-spotify-data/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.masalmon.eu/2017/10/01/bodypump/"> Maëlle</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I am a runner but also a Body Pump enthusiast. Body Pump is a group fitness class of the Les Mills company, in which you train different muscle groups using a weighted bar – whose total weight you modulate with plates in order to adapt it to your fitness level and to the muscle group. Like R, Body Pump was created in New Zealand, what a wonderful country! Every three months, a new class is released, with new songs and choreographies. What doesn’t change is the muscle group trained in each of the 10 songs of each class.</p>
<p>I’ve thought of analysing Body Pump data for a long time now but could never find what I was looking for, which was a dataset of number of “reps” by song, e.g. how many squats do you do in each squats song. Then I realized I could also play with other data, like a quite comprehensive list of songs used in releases. I decided to cross this information with information about style of the corresponding artist in Spotify. Here is what I came up with!</p>
<p><span></span></p>
<h1>Track 1: Track list cleaning</h1>
<p>This I did listening to pop music, actually using my husband’s Deezer account. I downloaded data from <a rel="nofollow" href="http://www.blogforumsupport.com/2008/bodypump2008.html" target="_blank">this page</a> which is quite exhaustive for many releases but well not formatted in a consistent way over releases, so I needed to be patient. A bit like when in the triceps track one does many half repetitions and feels one’s arms are going to fall off.</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;rvest&quot;</span><span>)</span><span>
</span><span>pg</span><span> </span><span>&lt;-</span><span> </span><span>&quot;http://www.blogforumsupport.com/2008/bodypump2008.html&quot;</span><span>
</span><span>pg_content</span><span> </span><span>&lt;-</span><span> </span><span>read_html</span><span>(</span><span>pg</span><span>,</span><span> </span><span>encoding</span><span> </span><span>=</span><span> </span><span>&quot;latin1&quot;</span><span>)</span><span>
</span><span>save</span><span>(</span><span>pg_content</span><span>,</span><span> </span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;data/pg_content.RData&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>Then I extracted the track lists, in a non very elegant way. Sometimes one just wants the job done…</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;magrittr&quot;</span><span>)</span><span>
</span><span>xtract_nodes</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>node</span><span>,</span><span> </span><span>css</span><span>)</span><span> </span><span>{</span><span>
</span><span>html_nodes</span><span>(</span><span>node</span><span>,</span><span> </span><span>css</span><span>)</span><span> </span><span>%>%</span><span> </span><span>html_text</span><span>(</span><span>trim</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>}</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>xtract_nodes</span><span>(</span><span>pg_content</span><span>,</span><span> </span><span>'p'</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>tracks</span><span>[</span><span>grepl</span><span>(</span><span>&quot;1&quot;</span><span>,</span><span> </span><span>tracks</span><span>)</span><span> </span><span>&amp;</span><span> </span><span>grepl</span><span>(</span><span>&quot;2&quot;</span><span>,</span><span> </span><span>tracks</span><span>)</span><span> </span><span>&amp;</span><span> </span><span>grepl</span><span>(</span><span>&quot;9&quot;</span><span>,</span><span> </span><span>tracks</span><span>)</span><span> </span><span>&amp;</span><span> </span><span>grepl</span><span>(</span><span>&quot;10&quot;</span><span>,</span><span> </span><span>tracks</span><span>)]</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>unlist</span><span>(</span><span>tracks</span><span>)</span><span>
</span></code></pre>
</div>
<p>What follows is scripted manual correction of some mistakes in the data, or things that makes later extraction of title vs. artist a bit complicated.</p>
<div>
<pre><code><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;04. Campione 2012 - DJ Flow05. 50 Ways to Say Goodbye - Train&quot;</span><span>,</span><span>
</span><span>&quot;04. Campione 2012 - DJ Flow \n 05. 50 Ways to Say Goodbye - Train&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;Upion Havoc09. She's so Mean&quot;</span><span>,</span><span>
</span><span>&quot;Upion Havoc \n 09. She's so Mean&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;07. How Deep Is Your Love -&quot;</span><span>,</span><span> </span><span>&quot;07. How Deep Is Your Love - Calvin Harris&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;08 Bumble Bee -&quot;</span><span>,</span><span> </span><span>&quot;08 Bumble Bee - Zedd &amp; Botnek&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;Young &amp; Stupid -&quot;</span><span>,</span><span> </span><span>&quot;Young &amp; Stupid - Travis Mills feat. T.I.&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;Bpn Jovi&quot;</span><span>,</span><span> </span><span>&quot; - Bon Jovi&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;Damn-R&quot;</span><span>,</span><span> </span><span>&quot;Damn R&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;Re-Dub&quot;</span><span>,</span><span> </span><span>&quot;Re Dub&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;Boom-Lay Boom-Lay Boom&quot;</span><span>,</span><span> </span><span>&quot;Boom Lay Boom Lay Boom&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;Hi-Tack&quot;</span><span>,</span><span> </span><span>&quot;Hi Tack&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;\\(Calvin Harris Remix\\) Fatboy Slim&quot;</span><span>,</span><span> </span><span>&quot;Fatboy Slim&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;B-Tastic&quot;</span><span>,</span><span> </span><span>&quot;B Tastic&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;E-Nergy&quot;</span><span>,</span><span> </span><span>&quot;E Nergy&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;In the Clear Six60&quot;</span><span>,</span><span> </span><span>&quot;In the Clear - Six60&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;Everybody Go! Vice&quot;</span><span>,</span><span> </span><span>&quot;Everybody Go! - Vice&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;My Love - feat. T.I. - Justin Timberlake&quot;</span><span>,</span><span>
</span><span>&quot;My Love - Justin Timberlake feat. T.I.&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;Original Release \\(PPCA\\)&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>tracks</span><span>[</span><span>!</span><span>stringr</span><span>::</span><span>str_detect</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>&quot;Get ready for what may be our fiercest release yet&quot;</span><span>)]</span><span>
</span></code></pre>
</div>
<p>After doing this I lied on the floor drenched in sweat / created a <code>tibble</code>.</p>
<div>
<pre><code><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>tibble</span><span>::</span><span>tibble</span><span>(</span><span>track</span><span> </span><span>=</span><span> </span><span>tracks</span><span>)</span><span>
</span><span>tracks</span><span>$</span><span>release</span><span> </span><span>&lt;-</span><span> </span><span>nrow</span><span>(</span><span>tracks</span><span>)</span><span>:</span><span>1</span><span>
</span></code></pre>
</div>
<p>Note that the release variable isn’t the actual release number, it’s more a time variable.</p>
<p>At this stage I had one string by release, which I split in one string by track.</p>
<div>
<pre><code><span>tracks</span><span>$</span><span>track</span><span> </span><span>&lt;-</span><span> </span><span>purrr</span><span>::</span><span>map</span><span>(</span><span>tracks</span><span>$</span><span>track</span><span>,</span><span> </span><span>stringr</span><span>::</span><span>str_split</span><span>,</span><span>
</span><span>pattern</span><span> </span><span>=</span><span> </span><span>&quot;\\\n&quot;</span><span>,</span><span> </span><span>simplify</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>tracks</span><span>$</span><span>track</span><span> </span><span>&lt;-</span><span> </span><span>purrr</span><span>::</span><span>map</span><span>(</span><span>tracks</span><span>$</span><span>track</span><span>,</span><span> </span><span>unlist</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>tidyr</span><span>::</span><span>unnest</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span>)</span><span>
</span></code></pre>
</div>
<p>The next step was keeping only the 10 first tracks by release, ignoring bonus and alternative tracks listed at the end. It also involved removing useless strings that were not actually songs.</p>
<div>
<pre><code><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot; 02. &quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot; 03. &quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot; 04. &quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot; 05. &quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot; 06. &quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot; 07. &quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot; 08. &quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot; 08. &quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot; 09. &quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot; 09. &quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot; 10. &quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot; 10. &quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot; Calvin Harris&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot; Zedd &amp; Botnek&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>!=</span><span> </span><span>&quot; Travis Mills feat. T.I.&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>group_by</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>release</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>song</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>n</span><span>())</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>song</span><span> </span><span>&lt;=</span><span> </span><span>10</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>ungroup</span><span>(</span><span>tracks</span><span>)</span><span>
</span><span>knitr</span><span>::</span><span>kable</span><span>(</span><span>tracks</span><span>[</span><span>1</span><span>:</span><span>10</span><span>,])</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>release</th>
<th>track</th>
<th>song</th>
</tr>
</thead>
<tbody>
<tr>
<td>42</td>
<td>01. Run Wild – Hardwell feat. Jake Reese</td>
<td>1</td>
</tr>
<tr>
<td>42</td>
<td>02. Go! (Vincent Price Remix) – Wolfpack &amp; Avancada</td>
<td>2</td>
</tr>
<tr>
<td>42</td>
<td>03. Spoken Word – Chase &amp; Status feat. George the Poet</td>
<td>3</td>
</tr>
<tr>
<td>42</td>
<td>04 .LRAD – Knife Party</td>
<td>4</td>
</tr>
<tr>
<td>42</td>
<td>05. Friday – PARRI</td>
<td>5</td>
</tr>
<tr>
<td>42</td>
<td>06. Jungle Youth – Young The Giant</td>
<td>6</td>
</tr>
<tr>
<td>42</td>
<td>07. Ocho Cinco – DJ Snake feat. Yellow Claw</td>
<td>7</td>
</tr>
<tr>
<td>42</td>
<td>08. Feel Your Love – Flux Pavilion &amp; NGHTMRE feat. Jamie Lewis</td>
<td>8</td>
</tr>
<tr>
<td>42</td>
<td>09. Closer – The Chainsmokers feat. Halsey</td>
<td>9</td>
</tr>
<tr>
<td>42</td>
<td>10. Love On The Brain – Rihanna</td>
<td>10</td>
</tr>
</tbody>
</table>
<p>After this I extracted the title and artist of each track. I’m not sure why I needed to change the encoding of the string for that, but before trying this solution I was bothered by there being different sorts of dashes and hyphens.</p>
<div>
<pre><code><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track</span><span> </span><span>=</span><span> </span><span>stringr</span><span>::</span><span>str_trim</span><span>(</span><span>track</span><span>))</span><span>
</span><span>split_title_artist</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>string</span><span>,</span><span> </span><span>release</span><span>){</span><span>
</span><span>track</span><span> </span><span>&lt;-</span><span> </span><span>string</span><span>
</span><span>Encoding</span><span>(</span><span>string</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>&quot;latin1&quot;</span><span>
</span><span># avoid some issues later
</span><span> </span><span>string</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>string</span><span>,</span><span> </span><span>&quot;â\u0080\u0099&quot;</span><span>,</span><span> </span><span>&quot;'&quot;</span><span>)</span><span>
</span><span>string</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>string</span><span>,</span><span> </span><span>&quot;ï¼\u0088&quot;</span><span>,</span><span> </span><span>&quot;(&quot;</span><span>)</span><span>
</span><span>string</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>string</span><span>,</span><span> </span><span>&quot;¼\u0089&quot;</span><span>,</span><span> </span><span>&quot;)&quot;</span><span>)</span><span>
</span><span>string</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>string</span><span>,</span><span> </span><span>&quot;`&quot;</span><span>,</span><span> </span><span>&quot;'&quot;</span><span>)</span><span>
</span><span>string</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>string</span><span>,</span><span> </span><span>&quot;â\u0080¦&quot;</span><span>,</span><span> </span><span>&quot;...&quot;</span><span>)</span><span>
</span><span>string</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>string</span><span>,</span><span> </span><span>&quot;DÃ©jÃ&quot;</span><span>,</span><span> </span><span>&quot;Déjà&quot;</span><span>)</span><span>
</span><span>string</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>string</span><span>,</span><span> </span><span>&quot;Bbn Jovi&quot;</span><span>,</span><span> </span><span>&quot;- Bon Jovi&quot;</span><span>)</span><span>
</span><span>string</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>string</span><span>,</span><span> </span><span>&quot;Marfia&quot;</span><span>,</span><span> </span><span>&quot;Mafia&quot;</span><span>)</span><span>
</span><span>string</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>string</span><span>,</span><span> </span><span>&quot;:&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span><span>string</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>string</span><span>,</span><span> </span><span>&quot;Diamond Eyes (Boom-Lay Boom-Lay Boom)&quot;</span><span>,</span><span> </span><span>&quot;Diamond Eyes (Boom Lay Boom Lay Boom)&quot;</span><span>)</span><span>
</span><span>string</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>string</span><span>,</span><span> </span><span>&quot;Studio-X&quot;</span><span>,</span><span> </span><span>&quot;Studio X&quot;</span><span>)</span><span>
</span><span># hyphens/dashes
</span><span> </span><span>string</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>string</span><span>,</span><span> </span><span>&quot;â\u0080\u0093&quot;</span><span>,</span><span> </span><span>&quot;-&quot;</span><span>)</span><span>
</span><span>string</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>string</span><span>,</span><span> </span><span>&quot;–&quot;</span><span>,</span><span> </span><span>&quot;-&quot;</span><span>)</span><span>
</span><span>string</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>string</span><span>,</span><span> </span><span>&quot;-&quot;</span><span>,</span><span> </span><span>&quot;\u0096&quot;</span><span>)</span><span>
</span><span>both</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_split</span><span>(</span><span>string</span><span>,</span><span> </span><span>pattern</span><span> </span><span>=</span><span> </span><span>&quot;\u0096&quot;</span><span>,</span><span>
</span><span>simplify</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>tibble</span><span>::</span><span>tibble</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>both</span><span>[</span><span>1</span><span>],</span><span>
</span><span>artist</span><span> </span><span>=</span><span> </span><span>both</span><span>[</span><span>2</span><span>],</span><span> </span><span>release</span><span> </span><span>=</span><span> </span><span>release</span><span>,</span><span>
</span><span>track</span><span> </span><span>=</span><span> </span><span>track</span><span>)</span><span>
</span><span>}</span><span>
</span><span>split_a_release</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>strings</span><span>,</span><span> </span><span>release</span><span>){</span><span>
</span><span>purrr</span><span>::</span><span>map_df</span><span>(</span><span>strings</span><span>,</span><span> </span><span>split_title_artist</span><span>,</span><span> </span><span>release</span><span>)</span><span>
</span><span>}</span><span>
</span><span>tracks_si</span><span> </span><span>&lt;-</span><span> </span><span>purrr</span><span>::</span><span>map2_df</span><span>(</span><span>tracks</span><span>$</span><span>track</span><span>,</span><span> </span><span>tracks</span><span>$</span><span>release</span><span>,</span><span> </span><span>split_a_release</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>left_join</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>tracks_si</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;release&quot;</span><span>,</span><span> </span><span>&quot;track&quot;</span><span>))</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>title</span><span>,</span><span> </span><span>&quot;^[0-1]*[0-9]* *\\.*&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>))</span><span>
</span></code></pre>
</div>
<p>When the artist was in fact two artists, I kept only the first one which I assumed to be more informative of the style of the song.</p>
<div>
<pre><code><span>first_artist</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>artist</span><span>){</span><span>
</span><span>artist</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>artist</span><span>,</span><span> </span><span>&quot;\\&amp;.*$&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span><span>first_artist</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>artist</span><span>,</span><span> </span><span>&quot;[fF]eat.*&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span><span>if</span><span>(</span><span>stringr</span><span>::</span><span>str_detect</span><span>(</span><span>first_artist</span><span>,</span><span> </span><span>&quot;Colourbox&quot;</span><span>)){</span><span>
</span><span>first_artist</span><span> </span><span>&lt;-</span><span> </span><span>&quot;Colourbox&quot;</span><span>
</span><span>}</span><span>
</span><span>first_artist</span><span>
</span><span>}</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>first_artist</span><span> </span><span>=</span><span> </span><span>first_artist</span><span>(</span><span>tracks</span><span>$</span><span>artist</span><span>))</span><span>
</span></code></pre>
</div>
<p>And I got there:</p>
<div>
<pre><code><span>knitr</span><span>::</span><span>kable</span><span>(</span><span>tracks</span><span>[</span><span>1</span><span>:</span><span>10</span><span>,])</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>release</th>
<th>track</th>
<th>song</th>
<th>title</th>
<th>artist</th>
<th>first_artist</th>
</tr>
</thead>
<tbody>
<tr>
<td>42</td>
<td>01. Run Wild – Hardwell feat. Jake Reese</td>
<td>1</td>
<td>Run Wild</td>
<td>Hardwell feat. Jake Reese</td>
<td>Hardwell</td>
</tr>
<tr>
<td>42</td>
<td>02. Go! (Vincent Price Remix) – Wolfpack &amp; Avancada</td>
<td>2</td>
<td>Go! (Vincent Price Remix)</td>
<td>Wolfpack &amp; Avancada</td>
<td>Wolfpack</td>
</tr>
<tr>
<td>42</td>
<td>03. Spoken Word – Chase &amp; Status feat. George the Poet</td>
<td>3</td>
<td>Spoken Word</td>
<td>Chase &amp; Status feat. George the Poet</td>
<td>Chase</td>
</tr>
<tr>
<td>42</td>
<td>04 .LRAD – Knife Party</td>
<td>4</td>
<td>LRAD</td>
<td>Knife Party</td>
<td>Knife Party</td>
</tr>
<tr>
<td>42</td>
<td>05. Friday – PARRI</td>
<td>5</td>
<td>Friday</td>
<td>PARRI</td>
<td>PARRI</td>
</tr>
<tr>
<td>42</td>
<td>06. Jungle Youth – Young The Giant</td>
<td>6</td>
<td>Jungle Youth</td>
<td>Young The Giant</td>
<td>Young The Giant</td>
</tr>
<tr>
<td>42</td>
<td>07. Ocho Cinco – DJ Snake feat. Yellow Claw</td>
<td>7</td>
<td>Ocho Cinco</td>
<td>DJ Snake feat. Yellow Claw</td>
<td>DJ Snake</td>
</tr>
<tr>
<td>42</td>
<td>08. Feel Your Love – Flux Pavilion &amp; NGHTMRE feat. Jamie Lewis</td>
<td>8</td>
<td>Feel Your Love</td>
<td>Flux Pavilion &amp; NGHTMRE feat. Jamie Lewis</td>
<td>Flux Pavilion</td>
</tr>
<tr>
<td>42</td>
<td>09. Closer – The Chainsmokers feat. Halsey</td>
<td>9</td>
<td>Closer</td>
<td>The Chainsmokers feat. Halsey</td>
<td>The Chainsmokers</td>
</tr>
<tr>
<td>42</td>
<td>10. Love On The Brain – Rihanna</td>
<td>10</td>
<td>Love On The Brain</td>
<td>Rihanna</td>
<td>Rihanna</td>
</tr>
</tbody>
</table>
<h1>Track 2: Spotify data downloading</h1>
<p>I dreamt of a database giving me the beats per minute, style, etc of each song. I didn’t get any information about the bpm but thanks to the Spotify API and <a rel="nofollow" href="https://github.com/tiagomendesdantas/Rspotify" target="_blank">this R package</a> I was able to get information about the artists including their genres and popularity. For songs, the API does not give such information.</p>
<p>Note: the <code>spotifyOAuth</code> part of the following lines demands your first getting an Spotify API app client ID and secret as explained in the README of the package. I saved these as environment variables <a rel="nofollow" href="http://stat545.com/bit003_api-key-env-var.html" target="_blank">in my .Renviron</a>.</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;Rspotify&quot;</span><span>)</span><span>
</span><span>keys</span><span> </span><span>&lt;-</span><span> </span><span>spotifyOAuth</span><span>(</span><span>app_id</span><span> </span><span>=</span><span> </span><span>Sys.getenv</span><span>(</span><span>&quot;SPOTIFY_APPID&quot;</span><span>),</span><span>
</span><span>client_id</span><span> </span><span>=</span><span> </span><span>Sys.getenv</span><span>(</span><span>&quot;SPOTIFY_CLIENTID&quot;</span><span>),</span><span>
</span><span>client_secret</span><span> </span><span>=</span><span> </span><span>Sys.getenv</span><span>(</span><span>&quot;SPOTIFY_SECRET&quot;</span><span>))</span><span>
</span><span>get_and_wait</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>artist</span><span>,</span><span> </span><span>keys</span><span>){</span><span>
</span><span>Sys.sleep</span><span>(</span><span>1</span><span>)</span><span>
</span><span>info</span><span> </span><span>&lt;-</span><span> </span><span>searchArtist</span><span>(</span><span>artist</span><span>,</span><span> </span><span>token</span><span> </span><span>=</span><span> </span><span>keys</span><span>)</span><span>
</span><span>if</span><span>(</span><span>!</span><span>is.null</span><span>(</span><span>info</span><span>)){</span><span>
</span><span>info</span><span> </span><span>&lt;-</span><span> </span><span>info</span><span>[</span><span>1</span><span>,]</span><span>
</span><span>}</span><span>
</span><span>info</span><span>$</span><span>first_artist</span><span> </span><span>&lt;-</span><span> </span><span>artist</span><span>
</span><span>info</span><span>
</span><span>}</span><span>
</span><span>artists_info</span><span> </span><span>&lt;-</span><span> </span><span>purrr</span><span>::</span><span>map_df</span><span>(</span><span>unique</span><span>(</span><span>tracks</span><span>$</span><span>first_artist</span><span>),</span><span> </span><span>get_and_wait</span><span>,</span><span> </span><span>keys</span><span>)</span><span>
</span><span>readr</span><span>::</span><span>write_csv</span><span>(</span><span>artists_info</span><span>,</span><span> </span><span>path</span><span> </span><span>=</span><span> </span><span>&quot;data/artists_info.csv&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>left_join</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>artists_info</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;first_artist&quot;</span><span>)</span><span>
</span><span>readr</span><span>::</span><span>write_csv</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>path</span><span> </span><span>=</span><span> </span><span>&quot;data/tracks.csv&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>knitr</span><span>::</span><span>kable</span><span>(</span><span>artists_info</span><span>[</span><span>1</span><span>:</span><span>5</span><span>,])</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>display_name</th>
<th>id</th>
<th>popularity</th>
<th>followers</th>
<th>genres</th>
<th>type</th>
<th>first_artist</th>
</tr>
</thead>
<tbody>
<tr>
<td>Hardwell</td>
<td>6BrvowZBreEkXzJQMpL174</td>
<td>78</td>
<td>1660846</td>
<td>big room,deep big room,dutch house,edm,electro house,progressive electro house,progressive house,tropical house</td>
<td>artist</td>
<td>Hardwell</td>
</tr>
<tr>
<td>Wolfpack</td>
<td>74ycentHh1YZrW5x4PLnox</td>
<td>54</td>
<td>8554</td>
<td>crust punk,deep big room,sky room</td>
<td>artist</td>
<td>Wolfpack</td>
</tr>
<tr>
<td>Chase &amp; Status</td>
<td>3jNkaOXasoc7RsxdchvEVq</td>
<td>69</td>
<td>300912</td>
<td>brostep,deep groove house,drum and bass,edm,grime,house,tropical house</td>
<td>artist</td>
<td>Chase</td>
</tr>
<tr>
<td>Knife Party</td>
<td>2DuJi13MWHjRHrqRUwk8vH</td>
<td>64</td>
<td>529738</td>
<td>australian dance,big room,brostep,catstep,complextro,edm,electro house,electronic trap,moombahton,zapstep</td>
<td>artist</td>
<td>Knife Party</td>
</tr>
<tr>
<td>PARRI$</td>
<td>17L4AvcA7zS5xdnLhemEMM</td>
<td>40</td>
<td>7264</td>
<td>escape room</td>
<td>artist</td>
<td>PARRI</td>
</tr>
</tbody>
</table>
<p>I got a genres information for 209 out of 313 artists.</p>
<h1>Track 3: making sense of the genres of artists</h1>
<p>As one sees above, each artist can have several genres and there are a ton of possible genres. When googling a bit about Spotify music genres, I found <a rel="nofollow" href="http://everynoise.com/engenremap.html" target="_blank">this gem of a webpage</a>. Impressive! In my case, I wanted to have a few groups of musical genres in order to link them to the track, e.g. is there a musical genre more representative of the biceps track as opposed to other tracks?</p>
<p>I decided to cluster the artists based on the binary variables for each genre. For this I used the <code>klaR::kmodes</code> function because k-modes is a method for clustering based on categorical variables as opposed to k-means for instance. In work that I don’t present here, I played with 1) the number of clusters, looking at the resulting total sum of within-cluster differences and 2) the minimal number of artists that should have a genre for this genre to be included. For instance if a single artist has the style “francoton”, then I don’t want to use it for clustering. In the end, I was quite happy with 6 clusters and keeping only genres that occurred more than 10 times in the data. Doing this there were some artists I couldn’t cluster since they had only been described by very rare genres.</p>
<p>Before clustering I had to transform the data a bit.</p>
<div>
<pre><code><span>artists_info</span><span> </span><span>&lt;-</span><span> </span><span>readr</span><span>::</span><span>read_csv</span><span>(</span><span>&quot;data/artists_info.csv&quot;</span><span>)</span><span>
</span><span>artists_info</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>artists_info</span><span>,</span><span> </span><span>!</span><span>is.na</span><span>(</span><span>display_name</span><span>))</span><span>
</span><span>artists_info</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>artists_info</span><span>,</span><span> </span><span>-</span><span> </span><span>first_artist</span><span>,</span><span> </span><span>-</span><span> </span><span>followers</span><span>)</span><span>
</span><span>artists_info</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>artists_info</span><span>)</span><span>
</span><span># most important word?
</span><span>split_the_genres</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>genres</span><span>){</span><span>
</span><span>genres</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_split</span><span>(</span><span>genres</span><span>,</span><span> </span><span>&quot;,&quot;</span><span>)</span><span>
</span><span>genres</span><span>
</span><span>}</span><span>
</span><span>artists_info</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>artists_info</span><span>,</span><span> </span><span>!</span><span>is.na</span><span>(</span><span>genres</span><span>))</span><span>
</span><span>artists_info</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>artists_info</span><span>,</span><span> </span><span>genre</span><span> </span><span>=</span><span> </span><span>split_the_genres</span><span>(</span><span>genres</span><span>))</span><span>
</span><span>artists_info</span><span> </span><span>&lt;-</span><span> </span><span>tidyr</span><span>::</span><span>unnest</span><span>(</span><span>artists_info</span><span>,</span><span> </span><span>genre</span><span>)</span><span>
</span><span>artists_info</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>artists_info</span><span>,</span><span> </span><span>value</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>artists_info</span><span> </span><span>&lt;-</span><span> </span><span>tidyr</span><span>::</span><span>spread</span><span>(</span><span>unique</span><span>(</span><span>artists_info</span><span>),</span><span> </span><span>genre</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span># # keep only variables with more than 10 occurrence
</span><span>sum_or_not</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>){</span><span>
</span><span>if</span><span>(</span><span>is.logical</span><span>(</span><span>x</span><span>)){</span><span>
</span><span>sum</span><span>(</span><span>x</span><span>)</span><span> </span><span>></span><span> </span><span>10</span><span>
</span><span>}</span><span>else</span><span>{</span><span>
</span><span>TRUE</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>variables_info</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>summarise_all</span><span>(</span><span>artists_info</span><span>,</span><span> </span><span>sum_or_not</span><span>)</span><span>
</span><span>artists_info</span><span> </span><span>&lt;-</span><span> </span><span>artists_info</span><span>[,</span><span>as.logical</span><span>(</span><span>t</span><span>(</span><span>variables_info</span><span>))]</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>knitr</span><span>::</span><span>kable</span><span>(</span><span>artists_info</span><span>[</span><span>1</span><span>:</span><span>2</span><span>,])</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>display_name</th>
<th>id</th>
<th>popularity</th>
<th>genres</th>
<th>type</th>
<th>alternative metal</th>
<th>big room</th>
<th>brostep</th>
<th>bubblegum dance</th>
<th>dance pop</th>
<th>deep big room</th>
<th>edm</th>
<th>electro house</th>
<th>eurodance</th>
<th>europop</th>
<th>modern rock</th>
<th>neo mellow</th>
<th>nu metal</th>
<th>pop</th>
<th>pop christmas</th>
<th>pop punk</th>
<th>pop rap</th>
<th>pop rock</th>
<th>post-grunge</th>
<th>post-teen pop</th>
<th>progressive electro house</th>
<th>progressive house</th>
<th>r&amp;b</th>
<th>rock</th>
<th>tropical house</th>
</tr>
</thead>
<tbody>
<tr>
<td>3 Doors Down</td>
<td>2RTUTCvo6onsAnheUk3aL9</td>
<td>76</td>
<td>alternative metal,alternative rock,heavy christmas,nu metal,pop punk,pop rock,post-grunge,rap metal,rock</td>
<td>artist</td>
<td>TRUE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>TRUE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>TRUE</td>
<td>FALSE</td>
<td>TRUE</td>
<td>TRUE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>TRUE</td>
<td>FALSE</td>
</tr>
<tr>
<td>3OH!3</td>
<td>0FWzNDaEu9jdgcYTbcOa4F</td>
<td>68</td>
<td>dance pop,electropowerpop,modern rock,pop,pop punk,pop rap,post-teen pop</td>
<td>artist</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>TRUE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>TRUE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>TRUE</td>
<td>FALSE</td>
<td>TRUE</td>
<td>TRUE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>TRUE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>FALSE</td>
</tr>
</tbody>
</table>
<p>Then the clustering happened.</p>
<div>
<pre><code><span>set.seed</span><span>(</span><span>1</span><span>)</span><span>
</span><span>clustering</span><span> </span><span>&lt;-</span><span> </span><span>klaR</span><span>::</span><span>kmodes</span><span>(</span><span>artists_info</span><span>[,</span><span> </span><span>6</span><span>:</span><span>ncol</span><span>(</span><span>artists_info</span><span>)],</span><span> </span><span>modes</span><span> </span><span>=</span><span> </span><span>6</span><span>)</span><span>
</span><span>clustering</span><span>$</span><span>size</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## cluster
##  1  2  3  4  5  6 
## 17 13 22 14 21 84
</code></pre>
</div>
<div>
<pre><code><span>artists_info</span><span>$</span><span>cluster</span><span> </span><span>&lt;-</span><span> </span><span>clustering</span><span>$</span><span>cluster</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;magrittr&quot;</span><span>)</span><span>
</span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>artists_info</span><span>,</span><span> </span><span>cluster</span><span> </span><span>==</span><span> </span><span>1</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>display_name</span><span>,</span><span> </span><span>genres</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>head</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>knitr</span><span>::</span><span>kable</span><span>()</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>display_name</th>
<th>genres</th>
</tr>
</thead>
<tbody>
<tr>
<td>Armin van Buuren</td>
<td>big room,edm,progressive house,progressive trance,trance,tropical house</td>
</tr>
<tr>
<td>Avicii</td>
<td>big room,dance pop,edm,pop,tropical house</td>
</tr>
<tr>
<td>Bob Sinclar</td>
<td>disco house,edm,filter house,house,tropical house,vocal house</td>
</tr>
<tr>
<td>Calvin Harris</td>
<td>edm,electro house,house,pop,progressive house</td>
</tr>
<tr>
<td>Chase &amp; Status</td>
<td>brostep,deep groove house,drum and bass,edm,grime,house,tropical house</td>
</tr>
<tr>
<td>David Guetta</td>
<td>dance pop,edm,pop,tropical house</td>
</tr>
</tbody>
</table>
<p>I then tried to characterize each cluster by its most important genre, well one of them. It’s as arbitrary as the rest of the clustering but I needed to summarize information anyway. I’d be glad to hear about other ideas for categorizing artists!</p>
<div>
<pre><code><span>get_most_important_genre</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>df</span><span>){</span><span>
</span><span>counts</span><span> </span><span>&lt;-</span><span> </span><span>as.numeric</span><span>(</span><span>t</span><span>(</span><span>dplyr</span><span>::</span><span>summarize_all</span><span>(</span><span>df</span><span>[,</span><span> </span><span>6</span><span>:</span><span>(</span><span>ncol</span><span>(</span><span>df</span><span>)</span><span>-1</span><span>)],</span><span> </span><span>sum</span><span>)))</span><span>
</span><span>names</span><span>(</span><span>df</span><span>)[</span><span>6</span><span>:</span><span>(</span><span>ncol</span><span>(</span><span>df</span><span>)</span><span>-1</span><span>)][</span><span>which</span><span>(</span><span>counts</span><span> </span><span>==</span><span> </span><span>max</span><span>(</span><span>counts</span><span>))][</span><span>1</span><span>]</span><span>
</span><span>}</span><span>
</span><span>styles</span><span> </span><span>&lt;-</span><span> </span><span>artists_info</span><span> </span><span>%>%</span><span>
</span><span>split</span><span>(</span><span>artists_info</span><span>$</span><span>cluster</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>purrr</span><span>::</span><span>map_chr</span><span>(</span><span>get_most_important_genre</span><span>)</span><span>
</span><span>cluster_music</span><span> </span><span>&lt;-</span><span> </span><span>tibble</span><span>::</span><span>tibble</span><span>(</span><span>cluster</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>6</span><span>,</span><span>
</span><span>style</span><span> </span><span>=</span><span> </span><span>styles</span><span>)</span><span>
</span><span>knitr</span><span>::</span><span>kable</span><span>(</span><span>cluster_music</span><span>)</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>cluster</th>
<th>style</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>edm</td>
</tr>
<tr>
<td>2</td>
<td>big room</td>
</tr>
<tr>
<td>3</td>
<td>dance pop</td>
</tr>
<tr>
<td>4</td>
<td>alternative metal</td>
</tr>
<tr>
<td>5</td>
<td>pop</td>
</tr>
<tr>
<td>6</td>
<td>eurodance</td>
</tr>
</tbody>
</table>
<h1>Track 4: Plotting</h1>
<p>Once I had my categories for some artists, I was impatient to look at their proportions depending on the muscle group. I first prepared the data in order to have meaningful names for clusters and tracks.</p>
<div>
<pre><code><span>artists_info</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>artists_info</span><span>,</span><span> </span><span>display_name</span><span>,</span><span> </span><span>cluster</span><span>,</span><span> </span><span>id</span><span>,</span><span> </span><span>popularity</span><span>,</span><span>
</span><span>genres</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>left_join</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>artists_info</span><span>,</span><span>
</span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;display_name&quot;</span><span>,</span><span> </span><span>&quot;id&quot;</span><span>,</span><span> </span><span>&quot;popularity&quot;</span><span>,</span><span> </span><span>&quot;genres&quot;</span><span>))</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;ggplot2&quot;</span><span>)</span><span>
</span><span>theme_set</span><span>(</span><span>theme_gray</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>18</span><span>))</span><span>
</span><span>library</span><span>(</span><span>&quot;viridis&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;magrittr&quot;</span><span>)</span><span>
</span><span>track_muscle</span><span> </span><span>&lt;-</span><span> </span><span>tibble</span><span>::</span><span>tibble</span><span>(</span><span>song</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>10</span><span>,</span><span>
</span><span>muscle</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;warm-up&quot;</span><span>,</span><span> </span><span>&quot;squats&quot;</span><span>,</span><span> </span><span>&quot;chest&quot;</span><span>,</span><span>
</span><span>&quot;back&quot;</span><span>,</span><span> </span><span>&quot;triceps&quot;</span><span>,</span><span> </span><span>&quot;biceps&quot;</span><span>,</span><span>
</span><span>&quot;lunges&quot;</span><span>,</span><span> </span><span>&quot;shoulders&quot;</span><span>,</span><span> </span><span>&quot;core&quot;</span><span>,</span><span>
</span><span>&quot;cool-down&quot;</span><span>))</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>left_join</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>track_muscle</span><span>,</span><span>
</span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;song&quot;</span><span>)</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>tracks</span><span>,</span><span>
</span><span>muscle</span><span> </span><span>=</span><span> </span><span>forcats</span><span>::</span><span>fct_reorder</span><span>(</span><span>muscle</span><span>,</span><span> </span><span>song</span><span>))</span><span>
</span><span>tracks</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>left_join</span><span>(</span><span>tracks</span><span>,</span><span> </span><span>cluster_music</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;cluster&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>I first made a figure of the number of missing values i.e. missing cluster by muscle group. Sadly some muscle groups seem to have more missing values, hopefully the sample I do have for them is representative. Note that in total I have 42 releases.</p>
<div>
<pre><code><span>tracks</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>group_by</span><span>(</span><span>muscle</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>summarise</span><span>(</span><span>prop_na</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>is.na</span><span>(</span><span>cluster</span><span>))</span><span>/</span><span>n</span><span>())</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>aes</span><span>(</span><span>muscle</span><span>,</span><span> </span><span>prop_na</span><span>))</span><span> </span><span>+</span><span>
</span><span>ylab</span><span>(</span><span>&quot;Proportion of missing values&quot;</span><span>)</span><span>+</span><span>
</span><span>theme</span><span>(</span><span>axis.text.x</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>angle</span><span> </span><span>=</span><span> </span><span>90</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>))</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-19" src="https://i1.wp.com/www.masalmon.eu/figure/source/2017-10-01-bodypump/unnamed-chunk-19-1.png?w=456" data-recalc-dims="1"></p>
<p>And then I looked at the musical genres by muscle group.</p>
<div>
<pre><code><span>tracks</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>cluster</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>aes</span><span>(</span><span>muscle</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>style</span><span>),</span><span>
</span><span>position</span><span> </span><span>=</span><span> </span><span>&quot;fill&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_fill_viridis</span><span>(</span><span>discrete</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>axis.text.x</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>angle</span><span> </span><span>=</span><span> </span><span>90</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>))</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-20" src="https://i2.wp.com/www.masalmon.eu/figure/source/2017-10-01-bodypump/unnamed-chunk-20-1.png?w=456" data-recalc-dims="1"></p>
<p>So, is this interesting? I’m not exactly sure, ah! It seems that the shoulders track is associated with less pop or dance than the other tracks, maybe because overhead presses demand more rythm? Cool down does seem more quite with no electronic music (edm and big room) which makes sense. All in all, I was happy to see this plot but would like to have a better categorization of songs and well less missing values. I’m not a good musical analyst yet! Oh and I’d love to get data about beats per minute… Maybe in a next release of this post, by me or one of you dear readers? Oh and if you want music that’s more quiet, check out <a rel="nofollow" href="http://www.masalmon.eu/2017/04/30/radioedit/" target="_blank">this post of mine about Radio Swiss Classic program</a>!</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.masalmon.eu/2017/10/01/bodypump/"> Maëlle</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/lcy1cfNnqWg" height="1"></a></div>