The music of Les Mills Body Pump, with Spotify data

[This article was first published on Maëlle, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

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!

Track 1: Track list cleaning

This I did listening to pop music, actually using my husband’s Deezer account. I downloaded data from this page 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.

<span class="n">library</span><span class="p">(</span><span class="s2">"rvest"</span><span class="p">)</span><span class="w">
</span><span class="n">pg</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"http://www.blogforumsupport.com/2008/bodypump2008.html"</span><span class="w">
</span><span class="n">pg_content</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read_html</span><span class="p">(</span><span class="n">pg</span><span class="p">,</span><span class="w"> </span><span class="n">encoding</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"latin1"</span><span class="p">)</span><span class="w">
</span><span class="n">save</span><span class="p">(</span><span class="n">pg_content</span><span class="p">,</span><span class="w"> </span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"data/pg_content.RData"</span><span class="p">)</span><span class="w">
</span>

Then I extracted the track lists, in a non very elegant way. Sometimes one just wants the job done…

<span class="n">library</span><span class="p">(</span><span class="s2">"magrittr"</span><span class="p">)</span><span class="w">
</span><span class="n">xtract_nodes</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">node</span><span class="p">,</span><span class="w"> </span><span class="n">css</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">html_nodes</span><span class="p">(</span><span class="n">node</span><span class="p">,</span><span class="w"> </span><span class="n">css</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">html_text</span><span class="p">(</span><span class="n">trim</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">xtract_nodes</span><span class="p">(</span><span class="n">pg_content</span><span class="p">,</span><span class="w"> </span><span class="s1">'p'</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tracks</span><span class="p">[</span><span class="n">grepl</span><span class="p">(</span><span class="s2">"1"</span><span class="p">,</span><span class="w"> </span><span class="n">tracks</span><span class="p">)</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">grepl</span><span class="p">(</span><span class="s2">"2"</span><span class="p">,</span><span class="w"> </span><span class="n">tracks</span><span class="p">)</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">grepl</span><span class="p">(</span><span class="s2">"9"</span><span class="p">,</span><span class="w"> </span><span class="n">tracks</span><span class="p">)</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">grepl</span><span class="p">(</span><span class="s2">"10"</span><span class="p">,</span><span class="w"> </span><span class="n">tracks</span><span class="p">)]</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">unlist</span><span class="p">(</span><span class="n">tracks</span><span class="p">)</span><span class="w">
</span>

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.

<span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> 
                               </span><span class="s2">"04. Campione 2012 - DJ Flow05. 50 Ways to Say Goodbye - Train"</span><span class="p">,</span><span class="w">
                               </span><span class="s2">"04. Campione 2012 - DJ Flow \n 05. 50 Ways to Say Goodbye - Train"</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> 
                               </span><span class="s2">"Upion Havoc09. She's so Mean"</span><span class="p">,</span><span class="w">
                               </span><span class="s2">"Upion Havoc \n 09. She's so Mean"</span><span class="p">)</span><span class="w">

</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"07. How Deep Is Your Love -"</span><span class="p">,</span><span class="w"> </span><span class="s2">"07. How Deep Is Your Love - Calvin Harris"</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"08 Bumble Bee -"</span><span class="p">,</span><span class="w"> </span><span class="s2">"08 Bumble Bee - Zedd & Botnek"</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"Young & Stupid -"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Young & Stupid - Travis Mills feat. T.I."</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"Bpn Jovi"</span><span class="p">,</span><span class="w"> </span><span class="s2">" - Bon Jovi"</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"Damn-R"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Damn R"</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"Re-Dub"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Re Dub"</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"Boom-Lay Boom-Lay Boom"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Boom Lay Boom Lay Boom"</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"Hi-Tack"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Hi Tack"</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"\\(Calvin Harris Remix\\) Fatboy Slim"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Fatboy Slim"</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"B-Tastic"</span><span class="p">,</span><span class="w"> </span><span class="s2">"B Tastic"</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"E-Nergy"</span><span class="p">,</span><span class="w"> </span><span class="s2">"E Nergy"</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"In the Clear Six60"</span><span class="p">,</span><span class="w"> </span><span class="s2">"In the Clear - Six60"</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"Everybody Go! Vice"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Everybody Go! - Vice"</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"My Love - feat. T.I. - Justin Timberlake"</span><span class="p">,</span><span class="w">
                               </span><span class="s2">"My Love - Justin Timberlake feat. T.I."</span><span class="p">)</span><span class="w">
 
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"Original Release \\(PPCA\\)"</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tracks</span><span class="p">[</span><span class="o">!</span><span class="n">stringr</span><span class="o">::</span><span class="n">str_detect</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="s2">"Get ready for what may be our fiercest release yet"</span><span class="p">)]</span><span class="w">
</span>

After doing this I lied on the floor drenched in sweat / created a tibble.

<span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tibble</span><span class="o">::</span><span class="n">tibble</span><span class="p">(</span><span class="n">track</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tracks</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="o">$</span><span class="n">release</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">nrow</span><span class="p">(</span><span class="n">tracks</span><span class="p">)</span><span class="o">:</span><span class="m">1</span><span class="w">
</span>

Note that the release variable isn’t the actual release number, it’s more a time variable.

At this stage I had one string by release, which I split in one string by track.

<span class="n">tracks</span><span class="o">$</span><span class="n">track</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">purrr</span><span class="o">::</span><span class="n">map</span><span class="p">(</span><span class="n">tracks</span><span class="o">$</span><span class="n">track</span><span class="p">,</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_split</span><span class="p">,</span><span class="w">
                     </span><span class="n">pattern</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"\\\n"</span><span class="p">,</span><span class="w"> </span><span class="n">simplify</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="o">$</span><span class="n">track</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">purrr</span><span class="o">::</span><span class="n">map</span><span class="p">(</span><span class="n">tracks</span><span class="o">$</span><span class="n">track</span><span class="p">,</span><span class="w"> </span><span class="n">unlist</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tidyr</span><span class="o">::</span><span class="n">unnest</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="p">)</span><span class="w">
</span>

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.

<span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">""</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"              02. "</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"              03. "</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"              04. "</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"              05. "</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"              06. "</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"              07. "</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"              08. "</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"            08. "</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"              09. "</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"            09. "</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"              10. "</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"            10. "</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"          Calvin Harris"</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"          Zedd & Botnek"</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"          Travis Mills feat. T.I."</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">group_by</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">release</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">mutate</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">song</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="o">:</span><span class="n">n</span><span class="p">())</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">song</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="m">10</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">ungroup</span><span class="p">(</span><span class="n">tracks</span><span class="p">)</span><span class="w">
</span><span class="n">knitr</span><span class="o">::</span><span class="n">kable</span><span class="p">(</span><span class="n">tracks</span><span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="m">10</span><span class="p">,])</span><span class="w">
</span>
release track song
42 01. Run Wild – Hardwell feat. Jake Reese 1
42 02. Go! (Vincent Price Remix) – Wolfpack & Avancada 2
42 03. Spoken Word – Chase & Status feat. George the Poet 3
42 04 .LRAD – Knife Party 4
42 05. Friday – PARRI 5
42 06. Jungle Youth – Young The Giant 6
42 07. Ocho Cinco – DJ Snake feat. Yellow Claw 7
42 08. Feel Your Love – Flux Pavilion & NGHTMRE feat. Jamie Lewis 8
42 09. Closer – The Chainsmokers feat. Halsey 9
42 10. Love On The Brain – Rihanna 10

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.

<span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">mutate</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">track</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_trim</span><span class="p">(</span><span class="n">track</span><span class="p">))</span><span class="w">

</span><span class="n">split_title_artist</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="n">release</span><span class="p">){</span><span class="w">
  </span><span class="n">track</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">string</span><span class="w">
  </span><span class="n">Encoding</span><span class="p">(</span><span class="n">string</span><span class="p">)</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"latin1"</span><span class="w">
  </span><span class="c1"># avoid some issues later
</span><span class="w">  </span><span class="n">string</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="s2">"â\u0080\u0099"</span><span class="p">,</span><span class="w"> </span><span class="s2">"'"</span><span class="p">)</span><span class="w">
  </span><span class="n">string</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="s2">"ï¼\u0088"</span><span class="p">,</span><span class="w"> </span><span class="s2">"("</span><span class="p">)</span><span class="w">
  </span><span class="n">string</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="s2">"¼\u0089"</span><span class="p">,</span><span class="w"> </span><span class="s2">")"</span><span class="p">)</span><span class="w">
  </span><span class="n">string</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="s2">"`"</span><span class="p">,</span><span class="w"> </span><span class="s2">"'"</span><span class="p">)</span><span class="w">
  </span><span class="n">string</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="s2">"â\u0080¦"</span><span class="p">,</span><span class="w"> </span><span class="s2">"..."</span><span class="p">)</span><span class="w">
  </span><span class="n">string</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="s2">"DéjÃ"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Déjà"</span><span class="p">)</span><span class="w">
  </span><span class="n">string</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="s2">"Bbn Jovi"</span><span class="p">,</span><span class="w"> </span><span class="s2">"- Bon Jovi"</span><span class="p">)</span><span class="w">
  </span><span class="n">string</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="s2">"Marfia"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Mafia"</span><span class="p">)</span><span class="w">
  </span><span class="n">string</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="s2">":"</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">)</span><span class="w">
  </span><span class="n">string</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="s2">"Diamond Eyes (Boom-Lay Boom-Lay Boom)"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Diamond Eyes (Boom Lay Boom Lay Boom)"</span><span class="p">)</span><span class="w">
  </span><span class="n">string</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="s2">"Studio-X"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Studio X"</span><span class="p">)</span><span class="w">
  
  </span><span class="c1"># hyphens/dashes
</span><span class="w">  </span><span class="n">string</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="s2">"â\u0080\u0093"</span><span class="p">,</span><span class="w"> </span><span class="s2">"-"</span><span class="p">)</span><span class="w">
  </span><span class="n">string</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="s2">"–"</span><span class="p">,</span><span class="w"> </span><span class="s2">"-"</span><span class="p">)</span><span class="w">
  </span><span class="n">string</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="s2">"-"</span><span class="p">,</span><span class="w"> </span><span class="s2">"\u0096"</span><span class="p">)</span><span class="w">
  </span><span class="n">both</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_split</span><span class="p">(</span><span class="n">string</span><span class="p">,</span><span class="w"> </span><span class="n">pattern</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"\u0096"</span><span class="p">,</span><span class="w">
                             </span><span class="n">simplify</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w">
  </span><span class="n">tibble</span><span class="o">::</span><span class="n">tibble</span><span class="p">(</span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">both</span><span class="p">[</span><span class="m">1</span><span class="p">],</span><span class="w">
                </span><span class="n">artist</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">both</span><span class="p">[</span><span class="m">2</span><span class="p">],</span><span class="w"> 
                </span><span class="n">release</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">release</span><span class="p">,</span><span class="w">
                </span><span class="n">track</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">track</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">split_a_release</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">strings</span><span class="p">,</span><span class="w"> </span><span class="n">release</span><span class="p">){</span><span class="w">
  </span><span class="n">purrr</span><span class="o">::</span><span class="n">map_df</span><span class="p">(</span><span class="n">strings</span><span class="p">,</span><span class="w"> </span><span class="n">split_title_artist</span><span class="p">,</span><span class="w"> </span><span class="n">release</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">tracks_si</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">purrr</span><span class="o">::</span><span class="n">map2_df</span><span class="p">(</span><span class="n">tracks</span><span class="o">$</span><span class="n">track</span><span class="p">,</span><span class="w"> </span><span class="n">tracks</span><span class="o">$</span><span class="n">release</span><span class="p">,</span><span class="w"> </span><span class="n">split_a_release</span><span class="p">)</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">left_join</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">tracks_si</span><span class="p">,</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"release"</span><span class="p">,</span><span class="w"> </span><span class="s2">"track"</span><span class="p">))</span><span class="w">
</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">mutate</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">title</span><span class="p">,</span><span class="w"> </span><span class="s2">"^[0-1]*[0-9]* *\\.*"</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">))</span><span class="w">
</span>

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.

<span class="n">first_artist</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">artist</span><span class="p">){</span><span class="w">
  </span><span class="n">artist</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">artist</span><span class="p">,</span><span class="w"> </span><span class="s2">"\\&.*$"</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">)</span><span class="w">
  </span><span class="n">first_artist</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_replace</span><span class="p">(</span><span class="n">artist</span><span class="p">,</span><span class="w"> </span><span class="s2">"[fF]eat.*"</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">)</span><span class="w">
  </span><span class="k">if</span><span class="p">(</span><span class="n">stringr</span><span class="o">::</span><span class="n">str_detect</span><span class="p">(</span><span class="n">first_artist</span><span class="p">,</span><span class="w"> </span><span class="s2">"Colourbox"</span><span class="p">)){</span><span class="w">
    </span><span class="n">first_artist</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"Colourbox"</span><span class="w">
  </span><span class="p">}</span><span class="w">
  </span><span class="n">first_artist</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">mutate</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">first_artist</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">first_artist</span><span class="p">(</span><span class="n">tracks</span><span class="o">$</span><span class="n">artist</span><span class="p">))</span><span class="w">
</span>

And I got there:

<span class="n">knitr</span><span class="o">::</span><span class="n">kable</span><span class="p">(</span><span class="n">tracks</span><span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="m">10</span><span class="p">,])</span><span class="w">
</span>
release track song title artist first_artist
42 01. Run Wild – Hardwell feat. Jake Reese 1 Run Wild Hardwell feat. Jake Reese Hardwell
42 02. Go! (Vincent Price Remix) – Wolfpack & Avancada 2 Go! (Vincent Price Remix) Wolfpack & Avancada Wolfpack
42 03. Spoken Word – Chase & Status feat. George the Poet 3 Spoken Word Chase & Status feat. George the Poet Chase
42 04 .LRAD – Knife Party 4 LRAD Knife Party Knife Party
42 05. Friday – PARRI 5 Friday PARRI PARRI
42 06. Jungle Youth – Young The Giant 6 Jungle Youth Young The Giant Young The Giant
42 07. Ocho Cinco – DJ Snake feat. Yellow Claw 7 Ocho Cinco DJ Snake feat. Yellow Claw DJ Snake
42 08. Feel Your Love – Flux Pavilion & NGHTMRE feat. Jamie Lewis 8 Feel Your Love Flux Pavilion & NGHTMRE feat. Jamie Lewis Flux Pavilion
42 09. Closer – The Chainsmokers feat. Halsey 9 Closer The Chainsmokers feat. Halsey The Chainsmokers
42 10. Love On The Brain – Rihanna 10 Love On The Brain Rihanna Rihanna

Track 2: Spotify data downloading

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 this R package I was able to get information about the artists including their genres and popularity. For songs, the API does not give such information.

Note: the spotifyOAuth 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 in my .Renviron.

<span class="n">library</span><span class="p">(</span><span class="s2">"Rspotify"</span><span class="p">)</span><span class="w">
</span><span class="n">keys</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">spotifyOAuth</span><span class="p">(</span><span class="n">app_id</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Sys.getenv</span><span class="p">(</span><span class="s2">"SPOTIFY_APPID"</span><span class="p">),</span><span class="w">
                     </span><span class="n">client_id</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Sys.getenv</span><span class="p">(</span><span class="s2">"SPOTIFY_CLIENTID"</span><span class="p">),</span><span class="w">
                     </span><span class="n">client_secret</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Sys.getenv</span><span class="p">(</span><span class="s2">"SPOTIFY_SECRET"</span><span class="p">))</span><span class="w">
</span><span class="n">get_and_wait</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">artist</span><span class="p">,</span><span class="w"> </span><span class="n">keys</span><span class="p">){</span><span class="w">
  </span><span class="n">Sys.sleep</span><span class="p">(</span><span class="m">1</span><span class="p">)</span><span class="w">
  </span><span class="n">info</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">searchArtist</span><span class="p">(</span><span class="n">artist</span><span class="p">,</span><span class="w"> </span><span class="n">token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">keys</span><span class="p">)</span><span class="w">
  </span><span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="nf">is.null</span><span class="p">(</span><span class="n">info</span><span class="p">)){</span><span class="w">
    </span><span class="n">info</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">info</span><span class="p">[</span><span class="m">1</span><span class="p">,]</span><span class="w">
  </span><span class="p">}</span><span class="w">
  </span><span class="n">info</span><span class="o">$</span><span class="n">first_artist</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">artist</span><span class="w">
  </span><span class="n">info</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">artists_info</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">purrr</span><span class="o">::</span><span class="n">map_df</span><span class="p">(</span><span class="n">unique</span><span class="p">(</span><span class="n">tracks</span><span class="o">$</span><span class="n">first_artist</span><span class="p">),</span><span class="w"> </span><span class="n">get_and_wait</span><span class="p">,</span><span class="w"> </span><span class="n">keys</span><span class="p">)</span><span class="w">
</span><span class="n">readr</span><span class="o">::</span><span class="n">write_csv</span><span class="p">(</span><span class="n">artists_info</span><span class="p">,</span><span class="w"> </span><span class="n">path</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"data/artists_info.csv"</span><span class="p">)</span><span class="w">

</span><span class="n">tracks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">left_join</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">artists_info</span><span class="p">,</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"first_artist"</span><span class="p">)</span><span class="w">
</span><span class="n">readr</span><span class="o">::</span><span class="n">write_csv</span><span class="p">(</span><span class="n">tracks</span><span class="p">,</span><span class="w"> </span><span class="n">path</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"data/tracks.csv"</span><span class="p">)</span><span class="w">

</span>
<span class="n">knitr</span><span class="o">::</span><span class="n">kable</span><span class="p">(</span><span class="n">artists_info</span><span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="m">5</span><span class="p">,])</span><span class="w">
</span>
display_name id popularity followers genres type first_artist
Hardwell 6BrvowZBreEkXzJQMpL174 78 1660846 big room,deep big room,dutch house,edm,electro house,progressive electro house,progressive house,tropical house artist Hardwell
Wolfpack 74ycentHh1YZrW5x4PLnox 54 8554 crust punk,deep big room,sky room artist Wolfpack
Chase & Status 3jNkaOXasoc7RsxdchvEVq 69 300912 brostep,deep groove house,drum and bass,edm,grime,house,tropical house artist Chase
Knife Party 2DuJi13MWHjRHrqRUwk8vH 64 529738 australian dance,big room,brostep,catstep,complextro,edm,electro house,electronic trap,moombahton,zapstep artist Knife Party
PARRI$ 17L4AvcA7zS5xdnLhemEMM 40 7264 escape room artist PARRI

I got a genres information for 209 out of 313 artists.

Track 3: making sense of the genres of artists

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 this gem of a webpage. 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?

I decided to cluster the artists based on the binary variables for each genre. For this I used the klaR::kmodes 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.

Before clustering I had to transform the data a bit.

<span class="n">artists_info</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">readr</span><span class="o">::</span><span class="n">read_csv</span><span class="p">(</span><span class="s2">"data/artists_info.csv"</span><span class="p">)</span><span class="w">
</span><span class="n">artists_info</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">artists_info</span><span class="p">,</span><span class="w"> </span><span class="o">!</span><span class="nf">is.na</span><span class="p">(</span><span class="n">display_name</span><span class="p">))</span><span class="w">
</span><span class="n">artists_info</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">select</span><span class="p">(</span><span class="n">artists_info</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">first_artist</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">followers</span><span class="p">)</span><span class="w">
</span><span class="n">artists_info</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">unique</span><span class="p">(</span><span class="n">artists_info</span><span class="p">)</span><span class="w">

</span><span class="c1"># most important word?
</span><span class="n">split_the_genres</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">genres</span><span class="p">){</span><span class="w">
  </span><span class="n">genres</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">stringr</span><span class="o">::</span><span class="n">str_split</span><span class="p">(</span><span class="n">genres</span><span class="p">,</span><span class="w"> </span><span class="s2">","</span><span class="p">)</span><span class="w">
  </span><span class="n">genres</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">artists_info</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">filter</span><span class="p">(</span><span class="n">artists_info</span><span class="p">,</span><span class="w"> </span><span class="o">!</span><span class="nf">is.na</span><span class="p">(</span><span class="n">genres</span><span class="p">))</span><span class="w">
</span><span class="n">artists_info</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">mutate</span><span class="p">(</span><span class="n">artists_info</span><span class="p">,</span><span class="w"> </span><span class="n">genre</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">split_the_genres</span><span class="p">(</span><span class="n">genres</span><span class="p">))</span><span class="w">
</span><span class="n">artists_info</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tidyr</span><span class="o">::</span><span class="n">unnest</span><span class="p">(</span><span class="n">artists_info</span><span class="p">,</span><span class="w"> </span><span class="n">genre</span><span class="p">)</span><span class="w">
</span><span class="n">artists_info</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">mutate</span><span class="p">(</span><span class="n">artists_info</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w">
</span><span class="n">artists_info</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tidyr</span><span class="o">::</span><span class="n">spread</span><span class="p">(</span><span class="n">unique</span><span class="p">(</span><span class="n">artists_info</span><span class="p">),</span><span class="w"> </span><span class="n">genre</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="p">,</span><span class="w"> </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">)</span><span class="w">
</span><span class="c1"># 
# keep only variables with more than 10 occurrence
</span><span class="n">sum_or_not</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">x</span><span class="p">){</span><span class="w">
  </span><span class="k">if</span><span class="p">(</span><span class="nf">is.logical</span><span class="p">(</span><span class="n">x</span><span class="p">)){</span><span class="w">
    </span><span class="nf">sum</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="m">10</span><span class="w">
  </span><span class="p">}</span><span class="k">else</span><span class="p">{</span><span class="w">
    </span><span class="kc">TRUE</span><span class="w">
  </span><span class="p">}</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="n">variables_info</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">dplyr</span><span class="o">::</span><span class="n">summarise_all</span><span class="p">(</span><span class="n">artists_info</span><span class="p">,</span><span class="w"> </span><span class="n">s...

To leave a comment for the author, please follow the link and comment on their blog: Maëlle.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)