<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/uk-2017-general-election-results-data/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://freigeist.devmag.net/r/1002-uk-2017-general-election-results-data.html"> fReigeist » R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>As the reality of a hung parliament starts to sink in, economists, political scientists and commentators will begin their usual routine of “post mortem” analysis of the surprise result of the UK 2017 general election. My co-authors Sascha Becker and Dennis Novy have done <a rel="nofollow" href="http://www.trfetzer.com/who-voted-for-brexit/" target="_blank">a similar exercise studying the EU Referendum last year</a> [see also <a rel="nofollow" href="http://voxeu.org/article/fundamental-factors-behind-brexit-vote" target="_blank">here</a>]  and have worked on the question whether migration contributed to an <a rel="nofollow" href="http://www.trfetzer.com/does-migration-cause-extreme-voting/" target="_blank">erosion of pro EU sentiment</a> [see also <a rel="nofollow" href="https://theconversation.com/immigration-continues-to-be-unpopular-but-cutting-it-will-hurt-britains-economy-78645" target="_blank">here</a>].</p>
<div>
<p>For people wanting to get to work straight away, there are a few things slowing us down.  The last constituency, Kensington, was not called until last night and so I dont expect the UK’s Election Commission to post the final tally of votes across all constituencies anytime before next week. Nevertheless, the crude election results data can be “scraped” from some infographics. This post describes how…</p>
<div>
<h3>The Economist’s Infographics</h3>
<p>The Economist, among other newspapers, provides a very nice infographic – behind that info graphic lies a web service that can be queried using JSON formed requests.</p>
<p><a rel="nofollow" href="https://i0.wp.com/www.trfetzer.com/wp-content/uploads/economist-infographic.png" target="_blank"><img alt="Economist Magazine Infographic presenting UK 2017 Election Results" data-id="771" src="https://i2.wp.com/www.trfetzer.com/wp-content/uploads/economist-infographic-1024x576.png?w=450" class="wp-image-771" data-recalc-dims="1"></a></p>
<p>Each Parliamentary constituency has an identifier code that can be used to query the web service and pull the results. The URL for a request is quite simple:</p>
<p><a rel="nofollow" href="http://infographics.economist.com/2017/ukelmap-2017/data/live-results2017/r2017E14000937.json" target="_blank">http://infographics.economist.com/2017/ukelmap-2017/data/live-results2017/r2017E14000937.json</a></p>
<p>This provides the results for the constituency Cambridgeshire, South East. The JSON object looks as follows</p>
<p><code>resultCB({&quot;swing&quot;: -3.84, &quot;mpn&quot;: &quot;Lucy Frazer&quot;, &quot;electorate&quot;: &quot;86121&quot;, &quot;lib&quot;: 11958, &quot;id&quot;: &quot;E14000937&quot;, &quot;name&quot;: &quot;Cambridgeshire, South East&quot;, &quot;lab&quot;: 17443, &quot;con&quot;: 33601, &quot;status&quot;: &quot;hold&quot;, &quot;pa_key&quot;: &quot;123&quot;, &quot;oth&quot;: 0, &quot;region&quot;: &quot;East Of England&quot;, &quot;win&quot;: &quot;con&quot;, &quot;turnout&quot;: &quot;63002&quot;})</code></p>
<p>This piece of Javascript calls a function <code>resultCB</code> that updates one of the views of the infographic.</p>
<p>In order to convert this to an R data frame, we can use the RJSONIO or jsonlite package functions fromJSON, after having removed the part that calls the function, i.e.</p>
<pre><code>library(jsonlite)
as.data.frame(fromJSON(gsub(&quot;\\)$&quot;,&quot;&quot;,gsub(&quot;resultCB\\(&quot;,&quot;&quot;,readLines(con=&quot;http://infographics.economist.com/2017/ukelmap-2017/data/live-results2017/r2017E14000937.json&quot;)))))</code></pre>
<pre><code>##          id pa_key oth                       name win status swing   lib
## 1 E14000937    123   0 Cambridgeshire, South East con   hold -3.84 11958
##            region         mpn electorate turnout   lab   con
## 1 East Of England Lucy Frazer      86121   63002 17443 33601</code></pre>
<p>In order to build a data.frame of all election results, all that is necessary is to loop over the set of constituency codes available. I share the results from this step in the following spreadsheet <a rel="nofollow" href="https://www.dropbox.com/s/onscjplojassbbh/EconomistUK2017.csv?dl=1" target="_blank">Data for UK 2017 General Election Results (Economist Infographic)</a>.</p>
</div>
</div>
<div>
<div></div>
</div>
	<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://freigeist.devmag.net/r/1002-uk-2017-general-election-results-data.html"> fReigeist » R</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/o3zU2psAqM8" height="1"></a></div>