Using the ONS postcode file to get the 2015 General Election Results by unit postcode

I was wanting to do some analysis on the results of the 2015 UK general election. In a lot of the datasets I use the only geographical marker I have is the unit postcode. So before I can do any analysis on the results I first need to map all of the UK constituencies onto a postcode list. Luckily for me this can be done using the ONS postcode file which has, amongst other things, the Westminster Electoral Constituencies for all UK unit postcodes. I would normally do this in SAS but since I'm learning R I thought I would do it there first.

First I need to get my data:

  • ONS postcode file

The most recent postcode file can be downloaded from the ONS website here. This contains one huge .csv file with lots of variables and a number of look up files, one of which is the Westminster Electoral Boundaries.

  • Election Results

Because I did this on the day of the election data in a nice tabular form wasn't that easy to come by but there was a almost complete list on Wikipedia which I converted into this .csv. I'm not certain on the accuracy of this data but it did look reasonable so it'll do until I find something better.

Now I need to import the data and merge it together:

And this creates this output (zipped).

I don't think I need to create as many intermediate data frames as I have but it works and that is what I'm going for at the moment.

I'll add some uses of this data once I've done more.

Leave a Reply

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