<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/a-classical-analysis-radio-swiss-classic-program/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://maelle.github.io/2017/04/23/radioswissclassic/"> Maëlle</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I am not a classical music expert at all, but I happen to have friends who are, and am even married to someone playing the cello (and the ukulele!). I appreciate listening to such music from time to time, in particular Baroque music. A friend made me discover <a target="_blank" rel="nofollow" href="http://www.radioswissclassic.ch/en">Radio Swiss classic</a>, an online radio playing classical music all day and all night long, with a quite nice variety, and very little speaking between pieces, with no ads (thank you, funders of the radio!). Besides, the voices telling me which piece has just been played are really soothing, so Radio Swiss classic is a good one in my opinion.</p>
<p>Today, instead of anxiously waiting for the results of the French presidential elections, I decided to download the program of the radio in the last years and have a quick look at it, since after all, the website says that the radio aims at relaxing people.</p>
<p><span></span></p>
<h1>Scraping the program</h1>
<p>My webscraping became a bit more elegant because I followed <a target="_blank" rel="nofollow" href="https://twitter.com/expersso/status/839395958316232704">the advice of EP alias expersso</a>, who by the way should really start blogging. I started downloading programs since September 2008 because that’s when I met the friend who told me about Radio Swiss Classic.</p>
<div>
<pre><code><span>dates</span><span> </span><span>&lt;-</span><span> </span><span>seq</span><span>(</span><span>from</span><span> </span><span>=</span><span> </span><span>lubridate</span><span>::</span><span>ymd</span><span>(</span><span>&quot;2008-09-01&quot;</span><span>),</span><span>
</span><span>to</span><span> </span><span>=</span><span> </span><span>lubridate</span><span>::</span><span>ymd</span><span>(</span><span>&quot;2017-04-22&quot;</span><span>),</span><span>
</span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;1 day&quot;</span><span>)</span><span>
</span><span>base_url</span><span> </span><span>&lt;-</span><span> </span><span>&quot;http://www.radioswissclassic.ch/en/music-programme/search/&quot;</span><span>
</span><span>get_one_day_program</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>date</span><span>,</span><span> </span><span>base_url</span><span>){</span><span>
</span><span># in order to see progress
</span><span> </span><span>message</span><span>(</span><span>date</span><span>)</span><span>
</span><span># build URL
</span><span> </span><span>date_as_string</span><span> </span><span>&lt;-</span><span> </span><span>as.character</span><span>(</span><span>date</span><span>)</span><span>
</span><span>date_as_string</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace_all</span><span>(</span><span>date_as_string</span><span>,</span><span> </span><span>&quot;-&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span><span>url</span><span> </span><span>&lt;-</span><span> </span><span>paste0</span><span>(</span><span>base_url</span><span>,</span><span> </span><span>date_as_string</span><span>)</span><span>
</span><span># read page
</span><span> </span><span>page</span><span> </span><span>&lt;-</span><span> </span><span>try</span><span>(</span><span>xml2</span><span>::</span><span>read_html</span><span>(</span><span>url</span><span>),</span><span>
</span><span>silent</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>if</span><span>(</span><span>is</span><span>(</span><span>page</span><span>,</span><span> </span><span>&quot;try-error&quot;</span><span>)){</span><span>
</span><span>message</span><span>(</span><span>&quot;horribly wrong&quot;</span><span>)</span><span>
</span><span>closeAllConnections</span><span>()</span><span>
</span><span>return</span><span>(</span><span>NULL</span><span>)</span><span>
</span><span>}</span><span>else</span><span>{</span><span>
</span><span># find all times, artists and pieces
</span><span> </span><span>times</span><span> </span><span>&lt;-</span><span> </span><span>xml2</span><span>::</span><span>xml_text</span><span>(</span><span>xml2</span><span>::</span><span>xml_find_all</span><span>(</span><span>page</span><span>,</span><span> </span><span>xpath</span><span>=</span><span>&quot;//span[@class='time hidden-xs']//text()&quot;</span><span>))</span><span>
</span><span>artists</span><span> </span><span>&lt;-</span><span> </span><span>xml2</span><span>::</span><span>xml_text</span><span>(</span><span>xml2</span><span>::</span><span>xml_find_all</span><span>(</span><span>page</span><span>,</span><span> </span><span>xpath</span><span>=</span><span>&quot;//span[@class='titletag']//text()&quot;</span><span>))</span><span>
</span><span>pieces</span><span> </span><span>&lt;-</span><span> </span><span>xml2</span><span>::</span><span>xml_text</span><span>(</span><span>xml2</span><span>::</span><span>xml_find_all</span><span>(</span><span>page</span><span>,</span><span> </span><span>xpath</span><span>=</span><span>&quot;//span[@class='artist']//text()&quot;</span><span>))</span><span>
</span><span># the last artist and piece are the current ones
</span><span> </span><span>artists</span><span> </span><span>&lt;-</span><span> </span><span>artists</span><span>[</span><span>1</span><span>:</span><span>(</span><span>length</span><span>(</span><span>artists</span><span>)</span><span> </span><span>-</span><span> </span><span>1</span><span>)]</span><span>
</span><span>pieces</span><span> </span><span>&lt;-</span><span> </span><span>pieces</span><span>[</span><span>1</span><span>:</span><span>(</span><span>length</span><span>(</span><span>pieces</span><span>)</span><span> </span><span>-</span><span> </span><span>1</span><span>)]</span><span>
</span><span># get a timedate from each time
</span><span> </span><span>timedates</span><span> </span><span>&lt;-</span><span> </span><span>paste</span><span>(</span><span>as.character</span><span>(</span><span>date</span><span>),</span><span> </span><span>times</span><span>)</span><span>
</span><span>timedates</span><span> </span><span>&lt;-</span><span> </span><span>lubridate</span><span>::</span><span>ymd_hm</span><span>(</span><span>timedates</span><span>)</span><span>
</span><span>timedates</span><span> </span><span>&lt;-</span><span> </span><span>lubridate</span><span>::</span><span>force_tz</span><span>(</span><span>timedates</span><span>,</span><span> </span><span>tz</span><span> </span><span>=</span><span> </span><span>&quot;Europe/Zurich&quot;</span><span>)</span><span>
</span><span># format the output
</span><span> </span><span>program</span><span> </span><span>&lt;-</span><span> </span><span>tibble</span><span>::</span><span>tibble</span><span>(</span><span>time</span><span> </span><span>=</span><span> </span><span>timedates</span><span>,</span><span>
</span><span>artist</span><span> </span><span>=</span><span> </span><span>artists</span><span>,</span><span>
</span><span>piece</span><span> </span><span>=</span><span> </span><span>pieces</span><span>)</span><span>
</span><span>return</span><span>(</span><span>program</span><span>)</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>programs</span><span> </span><span>&lt;-</span><span> </span><span>purrr</span><span>::</span><span>map</span><span>(</span><span>dates</span><span>,</span><span> </span><span>get_one_day_program</span><span>,</span><span> </span><span>base_url</span><span> </span><span>=</span><span> </span><span>base_url</span><span>)</span><span>
</span><span>programs</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>bind_rows</span><span>(</span><span>programs</span><span>)</span><span>
</span><span>save</span><span>(</span><span>programs</span><span>,</span><span> </span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;data/radioswissclassic_programs.RData&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>There were some days without any program on the website, for which the website said something was horribly wrong with the server.</p>
<div>
<pre><code><span>load</span><span>(</span><span>&quot;data/radioswissclassic_programs.RData&quot;</span><span>)</span><span>
</span><span>wegot</span><span> </span><span>&lt;-</span><span> </span><span>length</span><span>(</span><span>unique</span><span>(</span><span>lubridate</span><span>::</span><span>as_date</span><span>(</span><span>programs</span><span>$</span><span>time</span><span>)))</span><span>
</span><span>wewanted</span><span> </span><span>&lt;-</span><span> </span><span>length</span><span>(</span><span>seq</span><span>(</span><span>from</span><span> </span><span>=</span><span> </span><span>lubridate</span><span>::</span><span>ymd</span><span>(</span><span>&quot;2008-09-01&quot;</span><span>),</span><span>
</span><span>to</span><span> </span><span>=</span><span> </span><span>lubridate</span><span>::</span><span>ymd</span><span>(</span><span>&quot;2017-04-22&quot;</span><span>),</span><span>
</span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;1 day&quot;</span><span>))</span><span>
</span></code></pre>
</div>
<p>However, I got a program for approximately 0.96 of the days.</p>
<h1>Who are the most popular composers?</h1>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;magrittr&quot;</span><span>)</span><span>
</span><span>table</span><span>(</span><span>programs</span><span>$</span><span>artist</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>broom</span><span>::</span><span>tidy</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>arrange</span><span>(</span><span>desc</span><span>(</span><span>Freq</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>head</span><span>(</span><span>n</span><span> </span><span>=</span><span> </span><span>20</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>Var1</th>
<th>Freq</th>
</tr>
</thead>
<tbody>
<tr>
<td>Wolfgang Amadeus Mozart</td>
<td>37823</td>
</tr>
<tr>
<td>Ludwig van Beethoven</td>
<td>20936</td>
</tr>
<tr>
<td>Joseph Haydn</td>
<td>18140</td>
</tr>
<tr>
<td>Franz Schubert</td>
<td>15596</td>
</tr>
<tr>
<td>Antonio Vivaldi</td>
<td>14947</td>
</tr>
<tr>
<td>Johann Sebastian Bach</td>
<td>12003</td>
</tr>
<tr>
<td>Felix Mendelssohn-Bartholdy</td>
<td>11541</td>
</tr>
<tr>
<td>Antonin Dvorak</td>
<td>10265</td>
</tr>
<tr>
<td>Gioachino Rossini</td>
<td>9591</td>
</tr>
<tr>
<td>Frédéric Chopin</td>
<td>8470</td>
</tr>
<tr>
<td>Piotr Iljitsch Tchaikowsky</td>
<td>8092</td>
</tr>
<tr>
<td>Georg Friedrich Händel</td>
<td>7935</td>
</tr>
<tr>
<td>Tomaso Albinoni</td>
<td>6175</td>
</tr>
<tr>
<td>Gaetano Donizetti</td>
<td>5945</td>
</tr>
<tr>
<td>Giuseppe Verdi</td>
<td>5639</td>
</tr>
<tr>
<td>Johannes Brahms</td>
<td>5526</td>
</tr>
<tr>
<td>Johann Nepomuk Hummel</td>
<td>5439</td>
</tr>
<tr>
<td>Camille Saint-Saëns</td>
<td>5395</td>
</tr>
<tr>
<td>Luigi Boccherini</td>
<td>5130</td>
</tr>
<tr>
<td>Johann Christian Bach</td>
<td>4976</td>
</tr>
</tbody>
</table>
<p>I’ll have to admit that I don’t even know all the composers in this table but they’re actually all famous according to my live-in classical music expert. Radio Swiss classic allows listeners to rate pieces, so the most popular ones are programmed more often, and well I guess the person making the programs also tend to program famous composers quite often.</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;ggplot2&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;hrbrthemes&quot;</span><span>)</span><span>
</span><span>table</span><span>(</span><span>programs</span><span>$</span><span>artist</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>broom</span><span>::</span><span>tidy</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_histogram</span><span>(</span><span>aes</span><span>(</span><span>Freq</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_x_log10</span><span>()</span><span> </span><span>+</span><span>
</span><span>theme_ipsum</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>14</span><span>)</span><span> </span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-3" src="https://i1.wp.com/maelle.github.io/figure/source/2017-04-23-radioswissclassic/unnamed-chunk-3-1.png?w=456" data-recalc-dims="1"></p>
<p>Interestingly, but not that surprisingly I guess given the popularity of, say, Mozart, the distribution of occurrences by composers seems to be log-normally distributed.</p>
<h1>How long are pieces?</h1>
<p>On the website of Radio Swiss classic it is stated that pieces are longer in the evening than during the day, which I wanted to try and see. Because the program of the radio was not corrected for time changes (i.e. on 25 hour-days there are only 24 hours of music according to the online program), I shall only look at pieces whose duration is smaller than 60 minutes, which solves the issue of missing days at the same time.</p>
<div>
<pre><code><span>programs</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>arrange</span><span>(</span><span>programs</span><span>,</span><span> </span><span>time</span><span>)</span><span>
</span><span>programs</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>programs</span><span>,</span><span>
</span><span>duration</span><span> </span><span>=</span><span> </span><span>difftime</span><span>(</span><span>lead</span><span>(</span><span>time</span><span>,</span><span> </span><span>1</span><span>),</span><span>
</span><span>time</span><span>,</span><span>
</span><span>units</span><span> </span><span>=</span><span> </span><span>&quot;min&quot;</span><span>))</span><span>
</span><span>programs</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>programs</span><span>,</span><span>
</span><span>duration</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>duration</span><span> </span><span>></span><span> </span><span>60</span><span>,</span><span>
</span><span>NA</span><span>,</span><span> </span><span>duration</span><span>))</span><span>
</span><span>programs</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>programs</span><span>,</span><span>
</span><span>hour</span><span> </span><span>=</span><span> </span><span>as.factor</span><span>(</span><span>lubridate</span><span>::</span><span>hour</span><span>(</span><span>time</span><span>)))</span><span>
</span><span>programs</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_boxplot</span><span>(</span><span>aes</span><span>(</span><span>hour</span><span>,</span><span> </span><span>duration</span><span>))</span><span>+</span><span>
</span><span>theme_ipsum</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>14</span><span>)</span><span> </span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-4" src="https://i1.wp.com/maelle.github.io/figure/source/2017-04-23-radioswissclassic/unnamed-chunk-4-1.png?w=456" data-recalc-dims="1"></p>
<p>I don’t find the difference between day and night that striking, maybe I could try to define day and night to have a prettier figure (but I won’t do any test, I soon need to go watch TV).</p>
<div>
<pre><code><span>programs</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>night</span><span> </span><span>=</span><span> </span><span>(</span><span>lubridate</span><span>::</span><span>hour</span><span>(</span><span>time</span><span>)</span><span> </span><span>&lt;=</span><span> </span><span>4</span><span> </span><span>|</span><span> </span><span>lubridate</span><span>::</span><span>hour</span><span>(</span><span>time</span><span>)</span><span> </span><span>>=</span><span> </span><span>20</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_boxplot</span><span>(</span><span>aes</span><span>(</span><span>night</span><span>,</span><span> </span><span>duration</span><span>))</span><span>+</span><span>
</span><span>theme_ipsum</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>14</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-5" src="https://i2.wp.com/maelle.github.io/figure/source/2017-04-23-radioswissclassic/unnamed-chunk-5-1.png?w=456" data-recalc-dims="1"></p>
<h1>Conclusion</h1>
<p>The website also states that the pieces are more lively in the morning, but I have no data to which to match the titles of the pieces to investigate that claim. Well I have not even looked for such data. Another extension that I would find interesting would be to match each composer’s name to a style and then see how often each style is played. Now I’ll stop relaxing and go stuff my face with food in front of the election results!</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://maelle.github.io/2017/04/23/radioswissclassic/"> 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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/L7Rgph3SBEI"></a></div>