<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/clustering-music-genres-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://methodmatters.blogspot.com/2017/12/clustering-music-genres-with-r.html"> Method Matters</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In a number of upcoming posts, I’ll be analyzing an interesting dataset I found on Kaggle. The <a rel="nofollow" href="https://www.kaggle.com/nolanbconaway/pitchfork-data" target="_blank">dataset</a> contains information on 18,393 music reviews from the <a rel="nofollow" href="http://pitchfork.com/" target="_blank">Pitchfork</a> website. The data cover reviews posted between January 1999 and January 2016. I downloaded the data and did an extensive data munging exercise to turn the data into a tidy dataset for analysis (not described here, but perhaps in a future blog post).</p>
<p>The goal of this post is to describe the similarities and differences among the music genres of the reviewed albums using cluster analysis. </p>
<div><b>The Data</b></div>
<p>After data munging, I was left with 18,389 reviews for analysis (there were 4 identical review id’s in the dataset; visual inspection showed that the reviews were identical so I removed the duplicates).</p>
<p>One of the pieces of information about each album is the music genre, with the following options available: <i>electronic</i>, <i>experimental</i>, <i>folk/country</i>, <i>global</i>, <i>jazz</i>, <i>metal</i>,<i> pop/rnb</i>, <i>rap </i>and <i>rock</i>. Each album can have 0, 1 or multiple genres attached to it. I represented this information in the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Wide_and_narrow_data" target="_blank">wide format</a>, with one column to represent each genre. The presence of a given genre for a given album is represented with a 1, and the absence of a given genre for a given album is represented with a 0.</p>
<p>The head of the dataset, called “categories,” looks like this:</p>

<p></p>
<table>
<thead>
<tr>
<th>title</th>
<th>artist</th>
<th>electronic</th>
<th>experimental</th>
<th>folk_country</th>
<th>global</th>
<th>jazz</th>
<th>metal</th>
<th>pop_rnb</th>
<th>rap</th>
<th>rock</th>
</tr>
</thead>
<tbody>
<tr>
<td>mezzanine</td>
<td>massive attack</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>prelapsarian</td>
<td>krallice</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>all of them naturals</td>
<td>uranium club</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>first songs</td>
<td>kleenex liliput</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>new start</td>
<td>taso</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>insecure (music from the hbo original series)</td>
<td>various artists</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>As we can see, each album has numeric values on all of our 9 genres. The album in the last row of the data shown above does not have a genre attached to it.</p>
<p>Let’s first answer some basic questions about the music genres. How often is each genre represented in our 18,389 reviews? We can make a simple bar plot using base R with the following code:</p>
<pre><span># histogram of genre frequencies</span><br>par(<span>mai</span>=c(<span>1</span>,<span>1.5</span>,<span>1</span>,<span>1</span>))<br>barplot(sort(colSums(<span>categories</span>[,<span>3</span><span>:</span><span>11</span>]), <span>decreasing</span> <span>=</span> <span>FALSE</span>), <br>        <span>horiz</span> <span>=</span> <span>TRUE</span>, <span>cex.names</span> <span>=</span> <span>1</span>, <span>col</span> <span>=</span> <span>'springgreen4'</span>, <br>        <span>main</span> <span>=</span> <span>'Genre Frequency'</span>, <span>las</span> <span>=</span> <span>1</span>)<br><br></pre>
<p>Which gives us the following plot:</p>
<div><a rel="nofollow" href="https://i0.wp.com/2.bp.blogspot.com/-WkZS9sqS8_U/WgQi3vbWJnI/AAAAAAAAAXo/8x_O9wTd-tkaAnWXAQx_T77uCvM3_9wbQCLcBGAs/s1600/genre_frequencies.png?ssl=1" target="_blank"><img border="0" data-original-height="1600" src="https://i0.wp.com/2.bp.blogspot.com/-WkZS9sqS8_U/WgQi3vbWJnI/AAAAAAAAAXo/8x_O9wTd-tkaAnWXAQx_T77uCvM3_9wbQCLcBGAs/s1600/genre_frequencies.png?resize=450%2C1600&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<p>Rock is by far the most frequently-occurring genre in the dataset!</p>
<p>Let’s not forget that albums can have more than one genre. How many albums have more than 1 genre attached to them? What is the maximum number of genres in these data? What number of albums have what number of genres? It’s possible to extract these figures with <a rel="nofollow" href="https://cran.r-project.org/web/packages/dplyr/index.html" target="_blank"><b>dplyr</b></a>, but for basic summary statistics I find it quicker and easier to use base R:</p>
<pre><span># how many of the albums have more than 1 genre?</span><br>table(rowSums(<span>categories</span>[,<span>3</span><span>:</span><span>11</span>])<span>></span><span>1</span>)<br><span># FALSE TRUE </span><br><span># 14512 3877</span><br><br><span># what is the maximum number of genres?</span><br>max(rowSums(<span>categories</span>[,<span>3</span><span>:</span><span>11</span>]))<br><span># [1] 4</span><br><br><span># how many albums have what number of genres?</span><br>table(rowSums(<span>categories</span>[,<span>3</span><span>:</span><span>11</span>]))<br><span># 0 1 2 3 4 </span><br><span># 2365 12147 3500 345 32</span><br><br></pre>
<p>It looks like 3,877 of the albums have more than 1 genre. The table shows that the vast majority of albums with more than 1 genre have 2 genres, with a much smaller number having 3 and 4 genres. There are 2,365 albums with no recorded genre.</p>
<div><b>Data Preparation </b></div>
<p>In order to cluster the music genres, we first must make a matrix which contains the co-occurrences of our dummy-coded genre variables with one another. We can use matrix multiplication to accomplish this, as explained in this <a rel="nofollow" href="https://stackoverflow.com/questions/10622730/constructing-a-co-occurrence-matrix-from-dummycoded-observations-in-r" target="_blank">excellent StackOverflow answer</a>:</p>
<pre><span># make a co-occurrence matrix</span><br><span># select the relevant columns and convert to matrix format</span><br>library(<span>plyr</span>); library(<span>dplyr</span>)<br><br><span>co_occur</span> <span>&lt;-</span> <span>categories</span> %<span>></span>% select(<span>electronic</span>, <span>experimental</span>, <span>folk_country</span>, <br>              <span>global</span>,<span>jazz</span>, <span>metal</span>, <span>pop_rnb</span>, <span>rap</span>, <span>rock</span>) %<span>></span>% as.matrix() <br><span># calculate the co-occurrences of genres</span><br><span>out</span> <span>&lt;-</span> crossprod(<span>co_occur</span>)<br><span># make the diagonals of the matrix into zeros</span><br><span># (we won't count co-occurrences of a genre with itself)</span><br>diag(<span>out</span>) <span>&lt;-</span> <span>0</span> <br><br></pre>
<p>The resulting co-occurrence matrix, called “out”, is a 9 by 9 matrix containing the counts of the genre co-occurrences together in the data:</p>

<p></p>
<table>
<thead>
<tr>
<th></th>
<th>electronic</th>
<th>experimental</th>
<th>folk_country</th>
<th>global</th>
<th>jazz</th>
<th>metal</th>
<th>pop_rnb</th>
<th>rap</th>
<th>rock</th>
</tr>
</thead>
<tbody>
<tr>
<td>electronic</td>
<td>0</td>
<td>198</td>
<td>26</td>
<td>49</td>
<td>127</td>
<td>40</td>
<td>228</td>
<td>81</td>
<td>1419</td>
</tr>
<tr>
<td>experimental</td>
<td>198</td>
<td>0</td>
<td>15</td>
<td>20</td>
<td>76</td>
<td>64</td>
<td>32</td>
<td>17</td>
<td>1121</td>
</tr>
<tr>
<td>folk_country</td>
<td>26</td>
<td>15</td>
<td>0</td>
<td>7</td>
<td>6</td>
<td>10</td>
<td>65</td>
<td>2</td>
<td>52</td>
</tr>
<tr>
<td>global</td>
<td>49</td>
<td>20</td>
<td>7</td>
<td>0</td>
<td>13</td>
<td>0</td>
<td>48</td>
<td>3</td>
<td>33</td>
</tr>
<tr>
<td>jazz</td>
<td>127</td>
<td>76</td>
<td>6</td>
<td>13</td>
<td>0</td>
<td>26</td>
<td>52</td>
<td>25</td>
<td>57</td>
</tr>
<tr>
<td>metal</td>
<td>40</td>
<td>64</td>
<td>10</td>
<td>0</td>
<td>26</td>
<td>0</td>
<td>12</td>
<td>27</td>
<td>449</td>
</tr>
<tr>
<td>pop_rnb</td>
<td>228</td>
<td>32</td>
<td>65</td>
<td>48</td>
<td>52</td>
<td>12</td>
<td>0</td>
<td>133</td>
<td>126</td>
</tr>
<tr>
<td>rap</td>
<td>81</td>
<td>17</td>
<td>2</td>
<td>3</td>
<td>25</td>
<td>27</td>
<td>133</td>
<td>0</td>
<td>68</td>
</tr>
<tr>
<td>rock</td>
<td>1419</td>
<td>1121</td>
<td>52</td>
<td>33</td>
<td>57</td>
<td>449</td>
<td>126</td>
<td>68</td>
<td>0</td>
</tr>
</tbody>
</table>
<div><b>Cluster Analysis</b> </div>
<p>We can now proceed with the cluster analysis. We will use <a rel="nofollow" href="https://en.wikipedia.org/wiki/Hierarchical_clustering" target="_blank">hierarchical clustering</a>, an algorithm which seeks to build a hierarchy of clusters in the data. This analysis will produce groupings (e.g. clusters) of music genres. We will visualize the results of this analysis via a <a rel="nofollow" href="https://en.wikipedia.org/wiki/Dendrogram" target="_blank">dendrogram</a>.</p>
<p>We first need to produce a <a rel="nofollow" href="http://www.r-tutor.com/gpu-computing/clustering/distance-matrix" target="_blank">distance matrix</a> from our co-occurrence matrix. For each pair of music genres, we will calculate the Euclidean distance between them. To calculate the Euclidean distances, we first calculate the sum of the squared differences in co-occurrences for each pair of rows across the nine columns. We then take the square root of the sum of squared differences. If we consider the two bottom rows (rap and rock) in the co-occurrence matrix above, then the Euclidean distance between them is calculated as follows:</p>
<pre><span># calculate Euclidean distance manually between rock and rap</span><br><span>squared_diffs</span> <span>=</span> (<span>81</span><span>-</span><span>1419</span>)<span>^</span><span>2</span> <span>+</span> (<span>17</span><span>-</span><span>1121</span>)<span>^</span><span>2</span> <span>+</span> (<span>2</span><span>-</span><span>52</span>)<span>^</span><span>2</span> <span>+</span> (<span>3</span><span>-</span><span>33</span>)<span>^</span><span>2</span> <span>+</span> <br>      (<span>25</span><span>-</span><span>57</span>)<span>^</span><span>2</span> <span>+</span> (<span>27</span><span>-</span><span>449</span>)<span>^</span><span>2</span> <span>+</span> (<span>133</span><span>-</span><span>126</span>)<span>^</span><span>2</span> <span>+</span> (<span>0</span><span>-</span><span>68</span>)<span>^</span><span>2</span> <span>+</span> (<span>68</span><span>-</span><span>0</span>)<span>^</span><span>2</span><br>sqrt(<span>squared_diffs</span>)<br><span># [1] 1789.096</span><br><br></pre>
<p>This calculation makes clear our definition of genre similarity, which will define our clustering solution: two genres are similar to one another if they have similar patterns of co-occurrence with the other genres. </p>
<p>Let’s calculate all pairwise Euclidean distances with the <i>dist()</i> function, and check our manual calculation with the one produced by <i>dist()</i>.</p>
<pre><span># first produce a distance matrix</span><br><span># from our co-occurrence matrix</span><br><span>dist_matrix</span> <span>&lt;-</span> dist(<span>out</span>)<br><span># examine the distance matrix</span><br>round(<span>dist_matrix</span>,<span>3</span>)<br><span># the result for rap+rock</span><br><span># is 1789.096</span><br><span># the same as we calculated above!</span><br><br></pre>
<p>As noted in the R syntax above (and shown in the bottom-right corner of the distance matrix below), the distance between rap and rock is 1789.096, the same value that we obtained from our manual calculation above!</p>
<p>The distance matrix:</p>

<p></p>
<table>
<thead>
<tr>
<th></th>
<th>electronic</th>
<th>experimental</th>
<th>folk_country</th>
<th>global</th>
<th>jazz</th>
<th>metal</th>
<th>pop_rnb</th>
<th>rap</th>
</tr>
</thead>
<tbody>
<tr>
<td>experimental</td>
<td>462.453</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>folk_country</td>
<td>1397.729</td>
<td>1087.241</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>global</td>
<td>1418.07</td>
<td>1102.418</td>
<td>38.105</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>jazz</td>
<td>1392.189</td>
<td>1072.719</td>
<td>122.511</td>
<td>106.353</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>metal</td>
<td>1012.169</td>
<td>698.688</td>
<td>405.326</td>
<td>420.86</td>
<td>405.608</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>pop_rnb</td>
<td>1346.851</td>
<td>1006.476</td>
<td>275.647</td>
<td>259.559</td>
<td>197.16</td>
<td>397.776</td>
<td></td>
<td></td>
</tr>
<tr>
<td>rap</td>
<td>1375.999</td>
<td>1066.633</td>
<td>92.644</td>
<td>101.975</td>
<td>116.816</td>
<td>406.478</td>
<td>259.854</td>
<td></td>
</tr>
<tr>
<td>rock</td>
<td>2250.053</td>
<td>2041.001</td>
<td>1836.619</td>
<td>1818.557</td>
<td>1719.464</td>
<td>1854.881</td>
<td>1682.785</td>
<td>1789.096</td>
</tr>
</tbody>
</table>
<p>To perform the clustering, we simply pass the distance matrix to our hierarchical clustering algorithm (specifying that we to use <a rel="nofollow" href="https://en.wikipedia.org/wiki/Ward%27s_method" target="_blank">Ward’s method</a>), and produce the dendrogram plot.*</p>
<pre><span># perform the hierarchical clustering</span><br><span>hc</span> <span>&lt;-</span> hclust(<span>dist_matrix</span>, <span>method</span>=<span>&quot;ward.D&quot;</span>)<br><span># plot the dendrogram</span><br>plot(<span>hc</span>, <span>hang</span>=<span>-</span><span>1</span>, <span>xlab</span>=<span>&quot;&quot;</span>, <span>sub</span>=<span>&quot;&quot;</span>)<br><br></pre>
<p></p>
<div><a rel="nofollow" href="https://i1.wp.com/1.bp.blogspot.com/-KUsHV0u8-AE/WY7JX_7iZ6I/AAAAAAAAAWs/twhuZZNrHLUTeq6TRmS-nT5Dhm3kioSywCLcBGAs/s1600/basic_cluster.png?ssl=1" target="_blank"><img border="0" data-original-height="1600" src="https://i1.wp.com/1.bp.blogspot.com/-KUsHV0u8-AE/WY7JX_7iZ6I/AAAAAAAAAWs/twhuZZNrHLUTeq6TRmS-nT5Dhm3kioSywCLcBGAs/s1600/basic_cluster.png?resize=450%2C1600&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<p>The hierarchical clustering algorithm produces the organization of the music genres visible in the above dendrogram. But how many clusters are appropriate for these data? </p>
<p>There are <a rel="nofollow" href="https://stackoverflow.com/questions/15376075/cluster-analysis-in-r-determine-the-optimal-number-of-clusters" target="_blank">many different</a> <a rel="nofollow" href="https://en.wikipedia.org/wiki/Determining_the_number_of_clusters_in_a_data_set" target="_blank">ways of choosing</a> the number of clusters when performing cluster analysis. With hierarchical clustering, we can simply examine the dendrogram and make a decision about where to cut the tree to determine the number clusters for our solution. One of the key intuitions behind the dendrogram is that observations that fuse together higher up the tree (e.g. at the top of the plot) are more different from one another, while observations that fuse together further down (e.g. at the bottom of the plot) are more similar to one another. Therefore, the higher up we split the tree, the more different the music genres will be among the clusters.</p>
<p>If we split the tree near the top (e.g., around a height of 2500), we end up with three clusters. Let’s make a dendrogram that represents each of these three clusters with a different color. The colored dendrogram is useful in the interpretation of the cluster solution.</p>
<p>There are many different ways to produce a colored dendrogram in R; a lot of them are pretty hacky and require a fair amount of code. Below, I use the wonderful <b><a rel="nofollow" href="https://cran.r-project.org/web/packages/dendextend/index.html" target="_blank">dendextend</a> </b>package to produce the colored dendrogram in a straightforward and simple way (with a little help from this <a rel="nofollow" href="https://stackoverflow.com/questions/41097132/how-to-cut-a-dendrogram-in-r" target="_blank">StackOverflow answer</a>):</p>
<pre><span># make a colored dendrogram with the dendextend package</span><br>library(<span>dendextend</span>)<br><span># hc is the result of our hclust call above</span><br><span>dend</span> <span>&lt;-</span> <span>hc</span><br><span># specify we want to color the branches and labels</span><br><span>dend</span> <span>&lt;-</span> color_branches(<span>dend</span>, <span>k</span> <span>=</span> <span>3</span>)<br><span>dend</span> <span>&lt;-</span> color_labels(<span>dend</span>, <span>k</span> <span>=</span> <span>3</span>)<br><span># plot the colored dendrogram. add a title and y-axis label</span><br>plot(<span>dend</span>, <span>main</span> <span>=</span> <span>'Cluster Dendrogram'</span>, <span>ylab</span> <span>=</span> <span>'Height'</span>)<br><br></pre>
<p>Which yields the following plot:</p>
<div><a rel="nofollow" href="https://i0.wp.com/3.bp.blogspot.com/-OFosYP32e7M/WY89lemupoI/AAAAAAAAAXI/J-Sg0W7Yuo4LBVIblHIYBq0GmVns-27dQCLcBGAs/s1600/colored_cluster.png?ssl=1" target="_blank"><img border="0" data-original-height="1600" src="https://i0.wp.com/3.bp.blogspot.com/-OFosYP32e7M/WY89lemupoI/AAAAAAAAAXI/J-Sg0W7Yuo4LBVIblHIYBq0GmVns-27dQCLcBGAs/s1600/colored_cluster.png?resize=450%2C1600&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<div></div>
<p></p>
<div><b>Interpretation of the Clusters</b></div>
<p><i>The Clusters </i></p>
<p>The three clusters identified by our chosen cut point are as follows. First, on the right-hand side of the plot, we see <i>electronic </i>and <i>experimental </i>music grouped together in a single cluster (colored in blue). Also on the right-hand of the plot, on the same branch but with a different leaf and colored in green, we find <i>rock </i>music which constitutes a cluster in and of itself. These two clusters (<i>electronic + experimental</i> and <i>rock</i>) are distinct, yet they share some similarities in terms of their co-occurrences with other music genres, as indicated by their fusion at the top right-hand side of the dendrogram. </p>
<p>On the left-hand side of the dendrogram, we see the third cluster (colored in pink) which encompasses <i>metal</i>, <i>pop/r&amp;b</i>, <i>folk/country</i>, <i>global</i>, <i>jazz </i>and <i>rap </i>music. Our chosen cut point lumps all of these genres into a single cluster, but examination of the sub-divisions of this cluster reveals a more nuanced picture. <i>Metal </i>is most different from the other genres in this cluster. A further sub-grouping separates <i>pop/r&amp;b</i> from <i>folk/country</i>, <i>global</i>, <i>jazz </i>and <i>rap</i>. <i>Folk/country</i> and <i>global </i>are fused at the lowest point in the dendrogram, followed by a similar grouping of <i>jazz </i>and <i>rap</i>. These two pairings occur very close to the bottom of the dendrogram, indicating strong similarity in genre co-occurrence between <i>folk/country</i> and <i>global</i> on the one hand, and between <i>jazz </i>and <i>rap </i>on the other hand.<br><i><br></i><i>Substantive Interpretation: What have we learned about music genres?</i></p>
<p><u>Rock (Green Cluster)</u><br><i>Rock music</i> is the only music genre to get its own cluster, suggesting that it is distinctive in its patterns of co-occurrence with the other genres.</p>
<p>We must keep in mind the following statistical consideration, however. Rock music was the most frequent genre in the dataset. One of the reason rock’s Euclidean distances were so large is that, because this genre occurs so frequently in the data, the distances calculated between rock and the less-frequently occurring genres are naturally larger. </p>
<p><u>Electronic &amp; Experimental (Blue Cluster)</u><br>The blue cluster in the dendrogram above groups <i>electronic </i>and <i>experimental </i>music together. One reason for this might be because these genres are both relatively more modern (compared to, say, jazz or folk), and therefore share some sonic similarities (for example, using electronic or synthetically generated sounds) which leads them to be used in a similar way in conjunction with the other music genres. </p>
<p><u>Metal, Pop/R&amp;B, Folk/Country, Global, Jazz and Rap (Pink Cluster)</u> <br>The remaining genres fall into a single cluster. Within this cluster, it seems natural that <i>metal </i>is most different from the other genres, and that <i>pop/r&amp;b</i> separates itself from <i>folk/country</i>, <i>global</i>, <i>jazz </i>and <i>rap</i>.</p>
<p>Pop and R&amp;B are different, in my vision, from folk/country, global, jazz and rap music in that the former feature slick production styles (e.g. a tremendously layered sound with many different tracks making up a song), electronic instrumentation (e.g. keyboard synthesizers and drum machines) and a contemporary musical aesthetic (e.g. auto-tuning to produce noticeably distorted vocals), whereas the latter feature more sparse arrangements and fewer electronically-produced sounds.</p>
<p>Folk/country and global music, meanwhile, share a similar musical palette in terms of their more traditional instrumentation. While there are exceptions, I feel that both folk/country and global music use more acoustic or “natural” instruments (guitars, violins, wind instruments, drums, etc.) and less of the obviously electronically-produced sounds mentioned above. </p>
<p>Finally, jazz and hip-hop, although quite different in many musical aspects, share a similar aesthetic in some ways.  Hip-hop, for example, has long made use of samples from older records (including many jazz tunes) to create beats (although recent hip-hop sub-genres such as <a rel="nofollow" href="https://en.wikipedia.org/wiki/Trap_music" target="_blank">trap music</a> make use of drum machines and synthesizers for a more mechanical sound). One testament to the complementary between jazz and rap are number of recent notable collaborations between artists in these two genres: <a rel="nofollow" href="https://en.wikipedia.org/wiki/Kendrick_Lamar" target="_blank">Kendrick Lamar’s</a> excellent 2015 album <a rel="nofollow" href="https://en.wikipedia.org/wiki/To_Pimp_a_Butterfly" target="_blank"><i>To Pimp a Butterfly</i></a> contains a number of tracks featuring jazz musicians and jazz instrumentation, the jazz pianist <a rel="nofollow" href="https://en.wikipedia.org/wiki/Robert_Glasper" target="_blank">Robert Glasper</a> has made a <a rel="nofollow" href="https://en.wikipedia.org/wiki/Black_Radio" target="_blank">several</a> <a rel="nofollow" href="https://en.wikipedia.org/wiki/Black_Radio_2" target="_blank">collaborative albums</a> featuring rap artists, and the jazzy group <a rel="nofollow" href="https://en.wikipedia.org/wiki/BadBadNotGood" target="_blank">BadBadNotGood</a> made an <a rel="nofollow" href="https://en.wikipedia.org/wiki/Sour_Soul_(album)" target="_blank">excellent album</a> with the always brilliant <a rel="nofollow" href="https://en.wikipedia.org/wiki/Ghostface_Killah" target="_blank">Ghostface Killah</a>. </p>
<div><b>Conclusion</b></div>
<p>In this post, we clustered music genres from albums reviewed by Pitchfork. Our original dataset contained dummy-coded indicators for 9 different music genres across 18,389 different albums. We used matrix multiplication to create a co-occurrence matrix for the music genres, which we then turned into a distance matrix for hierarchical clustering. We cut the resulting dendrogram high up on the tree, obtaining three separate clusters of music genres: 1) <i>rock </i>2) <i>electronic </i>and <i>experimental </i>and 3) <i>metal</i>, <i>pop/r&amp;b</i>, <i>folk/country</i>, <i>global</i>, <i>jazz </i>and <i>rap</i>. This clustering solution seems to align with the production styles, sonic and musical qualities, and past and current cross-pollination between the various music genres.</p>
<p><i>Coming Up Next</i></p>
<p>In the next post, I’ll use R to produce a unique visualization of a very famous dataset. Stay tuned!</p>
<p>—-</p>
<p>* If you’re interested, you can play around with other distance metrics and clustering algorithms at home- just import the above co-occurrence matrix into R, and adapt the code however you like!</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://methodmatters.blogspot.com/2017/12/clustering-music-genres-with-r.html"> Method Matters</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/pRghMiq0vJM" height="1"></a></div>