<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/who-are-the-swedish-radio-p1-summer-guests-answer-via-wikidata/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.masalmon.eu/2017/08/06/p1/"> Maëlle</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This week, a <a rel="nofollow" href="https://eric.netlify.com/" target="_blank">very promising new R blog</a> was launched, namely the blog of Eric Persson a.k.a as <a rel="nofollow" href="https://twitter.com/expersso" target="_blank">expersso on Twitter</a>. I had really been looking forward to this because expersso’s code screenshots have always been <a rel="nofollow" href="https://twitter.com/expersso/status/870661615989002240" target="_blank">quite cool</a>, so seeing his no longer being limited to them is awesome! His first articles series is about a game, you should really <a rel="nofollow" href="https://eric.netlify.com/2017/08/04/implementing-nim-in-r/" target="_blank">check it out</a>. (<em>PSA: if you post screenshots of R code on Twitter, have a look at Sean Kross’ <a rel="nofollow" href="https://github.com/ropenscilabs/codefinch" target="_blank"><code>codefinch</code> package</a>!</em>).</p>
<p>Because I’m a nosy person I asked Eric whether he was Swedish, his last name being quite Swedish-looking in my opinion. He is, which made me wonder about Swedish blog topics and actually decided to use one Swedish topic I came up with, the <a rel="nofollow" href="http://sverigesradio.se/sida/artikel.aspx?programid=2071&artikel=4833403" target="_blank">summer guests of the Swedish radio P1</a>! Every summer since 1959, P1 selects a bunch of famous or interesting people and have them record a bit more than one hour program where they’re free to discuss what they want (important events of their life for instance) and to choose the musical breaks (which you don’t get to listen entirely to in the online version because of copyright stuff). The program is then broadcasted in the summer, one guest a day from the end of June to the beginning of August. There’s even a winter version now but I’ll ignore it because it’s too hot here in Barcelona to even think about winter.</p>
<p>It’s a very cool radio program in my opinion. I discovered it at the end of my 5-month research stay in Gothenburg in 2010 and decided it’d be one way to keep my Swedish skills up to date (my other methods include listening to ABBA in Swedish and reading Camilla Läckberg’s novels). I haven’t listened to that many guests but I really enjoy it when I do, and I like how diverse the list of guests is. In this post, I’ll actually try to have a look at the occupations of the guests via Wikidata!</p>
<p><span></span></p>
<h1>How to get the data?</h1>
<p>In order to answer my question I needed a list of all summer guests, and their occupations. I first thought I’d resort to webscraping P1 website, which would have been a good way to provide expersso with an opportunity to give me some <a rel="nofollow" href="https://twitter.com/expersso/status/839395958316232704" target="_blank">constructive</a> <a rel="nofollow" href="https://twitter.com/expersso/status/893530164482756609" target="_blank">feedback</a>. However after websurfing a bit to assess my options, I realized I could use Wikipedia data obtained through APIs rather than <a rel="nofollow" href="http://www.masalmon.eu/2017/02/12/wikideaths3_scraping/" target="_blank">webscraping as in my posts about famous dead people</a>. Here was my strategy.</p>
<h2>Getting the list of all summer guests from 1959</h2>
<p>I downloaded the table of all summer guests from 1959 from <a rel="nofollow" href="http://sverigesradio.se/sida/artikel.aspx?programid=2071&artikel=4833403" target="_blank">this page</a>, which gave me their names and the dates of their episode(s) – yeah some people are invited back.</p>
<div>
<pre><code><span># all summer guests
</span><span>sommargaester</span><span> </span><span>&lt;-</span><span> </span><span>readr</span><span>::</span><span>read_csv</span><span>(</span><span>&quot;data/p1sommar.csv&quot;</span><span>,</span><span> </span><span>col_names</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>locale</span><span> </span><span>=</span><span> </span><span>readr</span><span>::</span><span>locale</span><span>(</span><span>encoding</span><span> </span><span>=</span><span> </span><span>&quot;latin1&quot;</span><span>))</span><span>
</span><span>knitr</span><span>::</span><span>kable</span><span>(</span><span>sommargaester</span><span>[</span><span>1</span><span>:</span><span>10</span><span>,])</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>X1</th>
<th>X2</th>
<th>X3</th>
<th>X4</th>
<th>X5</th>
<th>X6</th>
<th>X7</th>
</tr>
</thead>
<tbody>
<tr>
<td>Aaro,Lars-Eric</td>
<td>110714</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>Abdelhadi,Magdi</td>
<td>070809</td>
<td>120716</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>Abdulle,Sherihan ‘Cherrie’</td>
<td>170706</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>Abele,Anton</td>
<td>130725</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>Abenius,Folke</td>
<td>800627</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>Abrahamson,Kjell-Albin</td>
<td>880809</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>Ackebo,Lena</td>
<td>970708</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>Adami,Zanyar</td>
<td>060707</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>Adamo,Amelia</td>
<td>860806</td>
<td>870718</td>
<td>080701</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>Adams,Maud</td>
<td>920703</td>
<td>070805</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
<td>NA</td>
</tr>
</tbody>
</table>
<div>
<pre><code><span># get their names
</span><span>sommargaester_names</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>sommargaester</span><span>$</span><span>X</span><span>1</span><span>)</span><span>
</span><span># for putting names in the right order for later queries
</span><span>transform_name</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>name</span><span>){</span><span>
</span><span>paste</span><span>(</span><span>stringr</span><span>::</span><span>str_split</span><span>(</span><span>name</span><span>,</span><span> </span><span>&quot;,&quot;</span><span>,</span><span>
</span><span>simplify</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)[</span><span>2</span><span>],</span><span>
</span><span>stringr</span><span>::</span><span>str_split</span><span>(</span><span>name</span><span>,</span><span> </span><span>&quot;,&quot;</span><span>,</span><span>
</span><span>simplify</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)[</span><span>1</span><span>])</span><span>
</span><span>}</span><span>
</span><span>pretty_sommargaester_names</span><span> </span><span>&lt;-</span><span> </span><span>purrr</span><span>::</span><span>map_chr</span><span>(</span><span>sommargaester_names</span><span>,</span><span> </span><span>transform_name</span><span>)</span><span>
</span><span>sommargaester_names</span><span> </span><span>&lt;-</span><span> </span><span>tibble</span><span>::</span><span>tibble</span><span>(</span><span>name</span><span> </span><span>=</span><span> </span><span>pretty_sommargaester_names</span><span>,</span><span>
</span><span>X</span><span>1</span><span> </span><span>=</span><span> </span><span>sommargaester_names</span><span>)</span><span>
</span><span>sommargaester</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>left_join</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>sommargaester_names</span><span>,</span><span>
</span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;X1&quot;</span><span>)</span><span>
</span><span>sommargaester</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>-</span><span> </span><span>X</span><span>1</span><span>)</span><span>
</span><span># transform the date
</span><span>sommargaester</span><span> </span><span>&lt;-</span><span> </span><span>tidyr</span><span>::</span><span>gather</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>&quot;rep&quot;</span><span>,</span><span> </span><span>&quot;date&quot;</span><span>,</span><span> </span><span>X</span><span>2</span><span>:</span><span>X</span><span>7</span><span>)</span><span>
</span><span>sommargaester</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>group_by</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>name</span><span>)</span><span>
</span><span>sommargaester</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>rep</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>n</span><span>())</span><span>
</span><span>sommargaester</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>ungroup</span><span>(</span><span>sommargaester</span><span>)</span><span>
</span><span>sommargaester</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>!</span><span>is.na</span><span>(</span><span>date</span><span>))</span><span>
</span><span># remove winter
</span><span>sommargaester</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>!</span><span>stringr</span><span>::</span><span>str_detect</span><span>(</span><span>date</span><span>,</span><span> </span><span>&quot;V&quot;</span><span>))</span><span>
</span><span># remove repeat episodes
</span><span>sommargaester</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>!</span><span>stringr</span><span>::</span><span>str_detect</span><span>(</span><span>date</span><span>,</span><span> </span><span>&quot;R&quot;</span><span>))</span><span>
</span><span># transform the date to a format with a non ambiguous year
</span><span>sommargaester</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>date</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>date</span><span>))</span><span>
</span><span>sommargaester</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>date</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>date</span><span> </span><span>></span><span> </span><span>180000</span><span>,</span><span> </span><span>paste0</span><span>(</span><span>&quot;19&quot;</span><span>,</span><span> </span><span>date</span><span>),</span><span>
</span><span>ifelse</span><span>(</span><span>date</span><span> </span><span></span><span> </span><span>100000</span><span>,</span><span>
</span><span>paste0</span><span>(</span><span>&quot;200&quot;</span><span>,</span><span> </span><span>date</span><span>),</span><span>
</span><span>paste0</span><span>(</span><span>&quot;20&quot;</span><span>,</span><span> </span><span>date</span><span>))))</span><span>
</span><span>sommargaester</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>pretty_date</span><span> </span><span>=</span><span> </span><span>lubridate</span><span>::</span><span>ymd</span><span>(</span><span>date</span><span>))</span><span>
</span><span>knitr</span><span>::</span><span>kable</span><span>(</span><span>sommargaester</span><span>[</span><span>1</span><span>:</span><span>10</span><span>,])</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>name</th>
<th>rep</th>
<th>date</th>
<th>pretty_date</th>
</tr>
</thead>
<tbody>
<tr>
<td>Lars-Eric Aaro</td>
<td>1</td>
<td>20110714</td>
<td>2011-07-14</td>
</tr>
<tr>
<td>Magdi Abdelhadi</td>
<td>1</td>
<td>20070809</td>
<td>2007-08-09</td>
</tr>
<tr>
<td>Sherihan ‘Cherrie’ Abdulle</td>
<td>1</td>
<td>20170706</td>
<td>2017-07-06</td>
</tr>
<tr>
<td>Anton Abele</td>
<td>1</td>
<td>20130725</td>
<td>2013-07-25</td>
</tr>
<tr>
<td>Folke Abenius</td>
<td>1</td>
<td>19800627</td>
<td>1980-06-27</td>
</tr>
<tr>
<td>Kjell-Albin Abrahamson</td>
<td>1</td>
<td>19880809</td>
<td>1988-08-09</td>
</tr>
<tr>
<td>Lena Ackebo</td>
<td>1</td>
<td>19970708</td>
<td>1997-07-08</td>
</tr>
<tr>
<td>Zanyar Adami</td>
<td>1</td>
<td>20060707</td>
<td>2006-07-07</td>
</tr>
<tr>
<td>Amelia Adamo</td>
<td>1</td>
<td>19860806</td>
<td>1986-08-06</td>
</tr>
<tr>
<td>Maud Adams</td>
<td>1</td>
<td>19920703</td>
<td>1992-07-03</td>
</tr>
</tbody>
</table>
<h2>Getting Wikidata about the summer guests</h2>
<p>Then I read the <a rel="nofollow" href="https://people.wikimedia.org/~bearloga/notes/r-pkgs.html" target="_blank">list of Wikipedia R packages</a> put together by <a rel="nofollow" href="https://twitter.com/bearloga" target="_blank">Mikhail Popov</a>, data scientist at the Wikimedia foundation with whom I exchanged a few messages after my Wikipedia deaths posts. These packages were either created by him or by <a rel="nofollow" href="https://twitter.com/awrno" target="_blank">Oliver Keyes</a> and really give you access to plenty of structured data so I was happy to at last give the list a try!</p>
<p>I used <a rel="nofollow" href="https://github.com/bearloga/WikidataQueryServiceR" target="_blank"><code>WikidataQueryServiceR</code></a> to access <a rel="nofollow" href="https://www.wikidata.org/wiki/Wikidata:Introduction" target="_blank">Wikidata</a> via the query language <code>SPARQL</code>. Luckily I didn’t need to actually learn <code>SPARQL</code>, I used the first example of the documentation, getting Douglas Adams’ data and played with <a rel="nofollow" href="https://query.wikidata.org/" target="_blank">this online query tool</a> to see how I could modify it to 1) obtain data in Swedish because Swedish Wikipedia is probably more complete about Swedish people and 2) obtain data about the summer guests, not Douglas Adams. For that second point I needed the item ID of each summer guests which I queried via another R package, <a rel="nofollow" href="https://github.com/Ironholds/WikidataR" target="_blank"><code>WikidataR</code></a>. I could have modified the <code>SPARQL</code> even more since I wouldn’t be using picture information but I wasn’t in a very perfectionist mood.</p>
<p>Doing all of this I only scraped the surface of the possibilities offered by the Wikipedia-related R packages and can already tell one could do a bunch of exciting analyses with them!</p>
<p>Note that I used <a rel="nofollow" href="https://rud.is/b/2017/04/23/decomposing-composers-with-r/" target="_blank">Bob Rudis’ code</a> as an example of how to insert a progress bar which made me feel quite cool. I also inserted a pause of 1 second between calls to the APIs in order to be a good person. My function name however shows how uninspired I was for naming things.</p>
<div>
<pre><code><span># function for getting someone's data
</span><span>get_someone_data</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>name</span><span>,</span><span> </span><span>pb</span><span> </span><span>=</span><span> </span><span>NULL</span><span>){</span><span>
</span><span>if</span><span> </span><span>(</span><span>!</span><span>is.null</span><span>(</span><span>pb</span><span>))</span><span> </span><span>pb</span><span>$</span><span>tick</span><span>()</span><span>$</span><span>print</span><span>()</span><span>
</span><span>Sys.sleep</span><span>(</span><span>1</span><span>)</span><span>
</span><span>item</span><span> </span><span>&lt;-</span><span> </span><span>WikidataR</span><span>::</span><span>find_item</span><span>(</span><span>name</span><span>,</span><span> </span><span>language</span><span> </span><span>=</span><span> </span><span>&quot;sv&quot;</span><span>)</span><span>
</span><span># sometimes people have no Wikidata entry so I need this condition
</span><span> </span><span>if</span><span>(</span><span>length</span><span>(</span><span>item</span><span>)</span><span> </span><span>></span><span> </span><span>0</span><span>){</span><span>
</span><span>entity_code</span><span> </span><span>&lt;-</span><span> </span><span>item</span><span>[[</span><span>1</span><span>]]</span><span>$</span><span>id</span><span>
</span><span>query</span><span> </span><span>&lt;-</span><span> </span><span>paste0</span><span>(</span><span>&quot;PREFIX entity: 
#partial results
SELECT ?propUrl ?propLabel ?valUrl ?valLabel ?picture
WHERE
{
hint:Query hint:optimizer 'None' .
{	BIND(entity:&quot;</span><span>,</span><span>entity_code</span><span>,</span><span>&quot; AS ?valUrl) .
BIND(\&quot;N/A\&quot; AS ?propUrl ) .
BIND(\&quot;identity\&quot;@sv AS ?propLabel ) .
}
UNION
{	entity:&quot;</span><span>,</span><span> </span><span>entity_code</span><span>,</span><span>&quot; ?propUrl ?valUrl .
?property ?ref ?propUrl .
?property rdf:type wikibase:Property .
?property rdfs:label ?propLabel
}
?valUrl rdfs:label ?valLabel
FILTER (LANG(?valLabel) = 'sv') .
OPTIONAL{ ?valUrl wdt:P18 ?picture .}
FILTER (lang(?propLabel) = 'sv' )
}
ORDER BY ?propUrl ?valUrl
LIMIT 200&quot;</span><span>)</span><span>
</span><span>results</span><span> </span><span>&lt;-</span><span> </span><span>WikidataQueryServiceR</span><span>::</span><span>query_wikidata</span><span>(</span><span>query</span><span>)</span><span>
</span><span>results</span><span>$</span><span>name</span><span>&lt;-</span><span> </span><span>name</span><span>
</span><span>results</span><span>
</span><span>}</span><span>else</span><span>{</span><span>
</span><span>NULL</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>pb</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>progress_estimated</span><span>(</span><span>length</span><span>(</span><span>unique</span><span>(</span><span>sommargaester</span><span>$</span><span>name</span><span>)))</span><span>
</span><span>sommargaester_wiki</span><span> </span><span>&lt;-</span><span> </span><span>purrr</span><span>::</span><span>map_df</span><span>(</span><span>unique</span><span>(</span><span>sommargaester</span><span>$</span><span>name</span><span>),</span><span>
</span><span>get_someone_data</span><span>,</span><span> </span><span>pb</span><span>=</span><span>pb</span><span>)</span><span>
</span><span>sommargaester</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>left_join</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>sommargaester_wiki</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;name&quot;</span><span>)</span><span>
</span><span>readr</span><span>::</span><span>write_csv</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>path</span><span> </span><span>=</span><span> </span><span>&quot;data/p1_wiki_data.csv&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>knitr</span><span>::</span><span>kable</span><span>(</span><span>sommargaester</span><span>[</span><span>1</span><span>:</span><span>10</span><span>,])</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>name</th>
<th>rep</th>
<th>pretty_date</th>
<th>propLabel</th>
<th>valLabel</th>
</tr>
</thead>
<tbody>
<tr>
<td>Lars-Eric Aaro</td>
<td>1</td>
<td>2011-07-14</td>
<td>arbetsgivare</td>
<td>LKAB</td>
</tr>
<tr>
<td>Lars-Eric Aaro</td>
<td>1</td>
<td>2011-07-14</td>
<td>födelseort</td>
<td>Vittangi</td>
</tr>
<tr>
<td>Lars-Eric Aaro</td>
<td>1</td>
<td>2011-07-14</td>
<td>kön</td>
<td>man</td>
</tr>
<tr>
<td>Lars-Eric Aaro</td>
<td>1</td>
<td>2011-07-14</td>
<td>nationalitet</td>
<td>Sverige</td>
</tr>
<tr>
<td>Lars-Eric Aaro</td>
<td>1</td>
<td>2011-07-14</td>
<td>instans av</td>
<td>människa</td>
</tr>
<tr>
<td>Lars-Eric Aaro</td>
<td>1</td>
<td>2011-07-14</td>
<td>alma mater</td>
<td>Luleå tekniska universitet</td>
</tr>
<tr>
<td>Lars-Eric Aaro</td>
<td>1</td>
<td>2011-07-14</td>
<td>medlem av</td>
<td>Kungliga Ingenjörsvetenskapsakademien</td>
</tr>
<tr>
<td>Lars-Eric Aaro</td>
<td>1</td>
<td>2011-07-14</td>
<td>förnamn</td>
<td>Lars</td>
</tr>
<tr>
<td>Lars-Eric Aaro</td>
<td>1</td>
<td>2011-07-14</td>
<td>identity</td>
<td>Lars-Eric Aaro</td>
</tr>
<tr>
<td>Magdi Abdelhadi</td>
<td>1</td>
<td>2007-08-09</td>
<td>sysselsättning</td>
<td>journalist</td>
</tr>
</tbody>
</table>
<p>I get one line per guest and propriety value, sometimes some propriety labels have more than one value for the same person, e.g. if the person has several occupations (occupation = sysselsättning).</p>
<h2>Translation the occupations</h2>
<p>This is the point at which I realized that maybe doing the search in English in the first place wouldn’t have made me get less data? Oh well. In any case for each possible occupation I can get an English translation which is awesome.</p>
<div>
<pre><code><span>occupation</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>propLabel</span><span> </span><span>==</span><span> </span><span>&quot;sysselsättning&quot;</span><span>)</span><span>
</span><span>occupations</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>occupation</span><span>$</span><span>valLabel</span><span>)</span><span>
</span><span>translate_occupation</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>occupation</span><span>){</span><span>
</span><span>job</span><span> </span><span>&lt;-</span><span> </span><span>WikidataR</span><span>::</span><span>find_item</span><span>(</span><span>occupation</span><span>,</span><span> </span><span>language</span><span> </span><span>=</span><span> </span><span>&quot;sv&quot;</span><span>)[[</span><span>1</span><span>]]</span><span>$</span><span>label</span><span>
</span><span>if</span><span>(</span><span>is.null</span><span>(</span><span>job</span><span>)){</span><span>
</span><span>job</span><span> </span><span>&lt;-</span><span> </span><span>&quot;&quot;</span><span>
</span><span>}</span><span>
</span><span>return</span><span>(</span><span>job</span><span>)</span><span>
</span><span>}</span><span>
</span><span>english_occupations</span><span> </span><span>&lt;-</span><span> </span><span>purrr</span><span>::</span><span>map_chr</span><span>(</span><span>occupations</span><span>,</span><span> </span><span>translate_occupation</span><span>)</span><span>
</span><span>translations</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>sysselsaettning</span><span> </span><span>=</span><span> </span><span>occupations</span><span>,</span><span>
</span><span>occupation</span><span> </span><span>=</span><span> </span><span>english_occupations</span><span>)</span><span>
</span><span>occupation</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>left_join</span><span>(</span><span>occupation</span><span>,</span><span> </span><span>translations</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;valLabel&quot;</span><span> </span><span>=</span><span> </span><span>&quot;sysselsaettning&quot;</span><span>))</span><span>
</span></code></pre>
</div>
<p>I guess it’s good to know that Wikidata has data in many languages because I can totally see it being useful as a sort of translation service.</p>
<h1>Profiling the summer guests</h1>
<h2>How much data?</h2>
<div>
<pre><code><span>withoccupation</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>!</span><span>is.na</span><span>(</span><span>propLabel</span><span>))</span><span>
</span><span>withoccupation</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>group_by</span><span>(</span><span>withoccupation</span><span>,</span><span> </span><span>name</span><span>)</span><span>
</span><span>withoccupation</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>summarise</span><span>(</span><span>withoccupation</span><span>,</span><span> </span><span>withoccupation</span><span> </span><span>=</span><span> </span><span>any</span><span>(</span><span>propLabel</span><span> </span><span>==</span><span> </span><span>&quot;sysselsättning&quot;</span><span>))</span><span>
</span></code></pre>
</div>
<p>There are 2192 unique guests in the dataset. We got Wikidata information for 1587 of them which means 72% of them. We got at least one occupation for 1421 guests which is 65% of them. At this point I have no way to know whether the sample is representative. Maybe the people with more Wikidata output are the most famous ones and some occupations probably help getting more famous than other so I tend to think this makes my sample a bit limited.</p>
<h2>How often do guests get invited</h2>
<p>As I said earlier some guests were invited several times. I want to look at the distribution of the number of invitations per guest.</p>
<div>
<pre><code><span>invites</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>sommargaester</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>rep</span><span>)</span><span>
</span><span>invites</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>group_by</span><span>(</span><span>invites</span><span>,</span><span> </span><span>name</span><span>)</span><span>
</span><span>invites</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>summarize</span><span>(</span><span>invites</span><span>,</span><span> </span><span>no_invites</span><span> </span><span>=</span><span> </span><span>max</span><span>(</span><span>rep</span><span>))</span><span>
</span><span>invites</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>invites</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;ggplot2&quot;</span><span>)</span><span>
</span><span>theme_set</span><span>(</span><span>theme_gray</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>18</span><span>))</span><span>
</span><span>ggplot</span><span>(</span><span>invites</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_histogram</span><span>(</span><span>aes</span><span>(</span><span>no_invites</span><span>))</span><span> </span><span>+</span><span>
</span><span>xlab</span><span>(</span><span>&quot;Number of invitations per P1 summer guest&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-5" src="https://i2.wp.com/www.masalmon.eu/figure/source/2017-08-06-p1/unnamed-chunk-5-1.png?w=456" data-recalc-dims="1"></p>
<p>Some people have been invited <em>a lot</em>! If you remember my initial tables, it had only a few columns for the episode dates but one guest could have several lines.</p>
<p>Who are the people that got invited that many times?</p>
<div>
<pre><code><span>jobs</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>occupation</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>occupation</span><span>)</span><span> </span><span>jobs</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>jobs</span><span>)</span><span>
</span><span>invites</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>left_join</span><span>(</span><span>invites</span><span>,</span><span> </span><span>occupation</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;name&quot;</span><span>)</span><span>
</span><span>invites</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>invites</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>no_invites</span><span>,</span><span> </span><span>occupation</span><span>)</span><span>
</span><span>invites</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>invites</span><span>)</span><span>
</span><span>invites</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>group_by</span><span>(</span><span>invites</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>no_invites</span><span>)</span><span>
</span><span>invites</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>summarise</span><span>(</span><span>invites</span><span>,</span><span> </span><span>occupation</span><span> </span><span>=</span><span> </span><span>toString</span><span>(</span><span>occupation</span><span>))</span><span>
</span><span>invites</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>ungroup</span><span>(</span><span>invites</span><span>)</span><span>
</span><span>invites</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>arrange</span><span>(</span><span>invites</span><span>,</span><span> </span><span>desc</span><span>(</span><span>no_invites</span><span>))</span><span>
</span><span>knitr</span><span>::</span><span>kable</span><span>(</span><span>invites</span><span>[</span><span>1</span><span>:</span><span>20</span><span>,])</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>name</th>
<th>no_invites</th>
<th>occupation</th>
</tr>
</thead>
<tbody>
<tr>
<td>Torsten Ehrenmark</td>
<td>54</td>
<td>journalist, translator, writer</td>
</tr>
<tr>
<td>Jörgen Cederberg</td>
<td>53</td>
<td>NA</td>
</tr>
<tr>
<td>Gösta Knutsson</td>
<td>42</td>
<td>cartoonist, journalist, translator, children’s writer, writer</td>
</tr>
<tr>
<td>Maud Reuterswärd</td>
<td>35</td>
<td>writer</td>
</tr>
<tr>
<td>Georg Eliasson</td>
<td>29</td>
<td>screenwriter</td>
</tr>
<tr>
<td>Pekka Langer</td>
<td>29</td>
<td>television presenter, actor</td>
</tr>
<tr>
<td>Britt Edwall</td>
<td>24</td>
<td>writer</td>
</tr>
<tr>
<td>Arne Ericsson</td>
<td>23</td>
<td>musician</td>
</tr>
<tr>
<td>Bengt Feldreich</td>
<td>23</td>
<td>journalist, television presenter</td>
</tr>
<tr>
<td>Berndt Friberg</td>
<td>23</td>
<td>journalist</td>
</tr>
<tr>
<td>Carl-Olof Lång</td>
<td>23</td>
<td>director</td>
</tr>
<tr>
<td>Carl-Uno Sjöblom</td>
<td>23</td>
<td>television presenter</td>
</tr>
<tr>
<td>Lars Ulvenstam</td>
<td>23</td>
<td>journalist</td>
</tr>
<tr>
<td>Lars Widding</td>
<td>23</td>
<td>journalist, writer</td>
</tr>
<tr>
<td>Åke Falck</td>
<td>17</td>
<td>film director, director, television presenter, screenwriter, actor</td>
</tr>
<tr>
<td>Barbro Alving</td>
<td>17</td>
<td>journalist, screenwriter, writer</td>
</tr>
<tr>
<td>Bo Setterlind</td>
<td>17</td>
<td>writer, poet</td>
</tr>
<tr>
<td>Bo Strömstedt</td>
<td>17</td>
<td>NA</td>
</tr>
<tr>
<td>Elisabeth Söderström</td>
<td>17</td>
<td>singer, opera singer</td>
</tr>
<tr>
<td>Fredrik Burgman</td>
<td>17</td>
<td>journalist</td>
</tr>
</tbody>
</table>
<h2>Most frequent occupations (by episode)</h2>
<div>
<pre><code><span>everything</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>group_by</span><span>(</span><span>occupation</span><span>,</span><span> </span><span>occupation</span><span>)</span><span>
</span><span>everything</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>summarize</span><span>(</span><span>everything</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>n</span><span>())</span><span>
</span><span>everything</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>everything</span><span>,</span><span> </span><span>n</span><span> </span><span>></span><span> </span><span>50</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;magrittr&quot;</span><span>)</span><span>
</span><span>everything</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>arrange</span><span>(</span><span>n</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>occupation</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>occupation</span><span>,</span><span> </span><span>ordered</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>unique</span><span>(</span><span>occupation</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_col</span><span>(</span><span>aes</span><span>(</span><span>occupation</span><span>,</span><span> </span><span>n</span><span>))</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>axis.text.x</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>angle</span><span> </span><span>=</span><span> </span><span>90</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>1</span><span>))</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Number of episodes by occupation&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;for occupations represented more than 50 times&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-7" src="https://i1.wp.com/www.masalmon.eu/figure/source/2017-08-06-p1/unnamed-chunk-7-1.png?w=456" data-recalc-dims="1"></p>
<p>So in terms of episodes I’d say the media and culture fields are quite well represented. That said it might also be due to the fact that a person doing sports for instance will be classified as either ice skater or sprinter, so there’s no chance for sports to appear here. I’ll leave the classification of occupations in categories as an exercise for the reader though.</p>
<h2>Most frequent occupations (by unique guest)</h2>
<div>
<pre><code><span>everything</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>occupation</span><span>,</span><span> </span><span>name</span><span>,</span><span> </span><span>occupation</span><span>)</span><span>
</span><span>everything</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>everything</span><span>)</span><span>
</span><span>everything</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>group_by</span><span>(</span><span>everything</span><span>,</span><span> </span><span>occupation</span><span>)</span><span>
</span><span>everything</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>summarize</span><span>(</span><span>everything</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>n</span><span>())</span><span>
</span><span>everything</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>everything</span><span>,</span><span> </span><span>n</span><span> </span><span>></span><span> </span><span>20</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;magrittr&quot;</span><span>)</span><span>
</span><span>everything</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>arrange</span><span>(</span><span>n</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>occupation</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>occupation</span><span>,</span><span> </span><span>ordered</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>unique</span><span>(</span><span>occupation</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_col</span><span>(</span><span>aes</span><span>(</span><span>occupation</span><span>,</span><span> </span><span>n</span><span>))</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>axis.text.x</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>angle</span><span> </span><span>=</span><span> </span><span>90</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>1</span><span>))</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Number of guests by occupation&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;for occupations represented more than 20 times&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-8" src="https://i2.wp.com/www.masalmon.eu/figure/source/2017-08-06-p1/unnamed-chunk-8-1.png?w=456" data-recalc-dims="1"></p>
<p>With this figure by guest rather than by episode, a bit more diversity has appeared, e.g. “university professor” and “association football player”. Moreover although this was a nice exercise I won’t pretend that someone’s occupation characterizes them fully! The P1 summer guest programm really presents a variety of episodes, and I’d tend to think everyone can find an episode that they appreciate. For instance I remember liking <a rel="nofollow" href="http://sverigesradio.se/sida/avsnitt/116698?programid=2071" target="_blank">the baker Sébastien Boudet’s episode</a> (yep he’s French) and <a rel="nofollow" href="http://sverigesradio.se/sida/avsnitt/369285?programid=2071" target="_blank">the one featuring the former high jumper Kajsa Berqvist</a>. Now all this data munging has left me more than ready to look at the list of episodes again to choose the next ones I’ll listen to!</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.masalmon.eu/2017/08/06/p1/"> Maëlle</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/uxMw5QoCcIg" height="1"></a></div>