UK 2017 General Election Results Data

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 similar exercise studying the EU Referendum last year [see also here]  and have worked on the question whether migration contributed to an erosion of pro EU sentiment [see also here].

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…

The Economist’s Infographics

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.

Economist Magazine Infographic presenting UK 2017 Election Results

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:

http://infographics.economist.com/2017/ukelmap-2017/data/live-results2017/r2017E14000937.json

This provides the results for the constituency Cambridgeshire, South East. The JSON object looks as follows

resultCB({"swing": -3.84, "mpn": "Lucy Frazer", "electorate": "86121", "lib": 11958, "id": "E14000937", "name": "Cambridgeshire, South East", "lab": 17443, "con": 33601, "status": "hold", "pa_key": "123", "oth": 0, "region": "East Of England", "win": "con", "turnout": "63002"})

This piece of Javascript calls a function resultCB that updates one of the views of the infographic.

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.

library(jsonlite)
as.data.frame(fromJSON(gsub("\\)$","",gsub("resultCB\\(","",readLines(con="http://infographics.economist.com/2017/ukelmap-2017/data/live-results2017/r2017E14000937.json")))))
##          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

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 Data for UK 2017 General Election Results (Economist Infographic).

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • MisterWong
  • Y!GG
  • Webnews
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit

4 thoughts on “UK 2017 General Election Results Data”

  1. Thanks for this, great information. Looks like something is off in your spreadsheet though. The first entry seems to list votes for the SNP – they did not stand in that constituency (Aberavon).

  2. Hi Thiemo,
    Thanks. I was unable to view the json easily so had not dug any deeper. Do you know where the electoral commission normally publishes their data? I was trying to find 2015 to no avail.
    Robin.

  3. Dear Robin,

    Yes unfortunately I found that the Electoral Commission is not very helpful in many respects as they do not collect any data beyond what they are legally required to do (such as collating data at a finer spatial resolution).

    The website is also a bit of a maze – usually data gets posted here (see the table as you scroll down)

    https://www.electoralcommission.org.uk/our-work/our-research/electoral-data

    Just getting back to the issue about Aberavon, it seems to be a data entry error on the the side of the Economist (you can just check out their infographic search for Aberavon and it will show you a 34% share for SNP.

    http://infographics.economist.com/2017/ukelmap-2017/

    It is a bit unfortunate that this is the first row in the spreadsheet, but the other information seems correct (labor tally, conservative count etc.), when comparing to other sources

    So I think the spreadsheet is quite “correct”, but this type of human error can happen.

    Best Thiemo

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Login with Facebook: