Introduction
The latest stable version of my UK geocoder package has finally made it to CRAN. PostcodesioR is a wrapper for postcodes.io and it provides multiple functions to work with UK geospatial data.
This package is based exclusively on open data provided by Ordnance Survey and Office for National Statistics and turned into an API by postcodes.io.
PostcodesioR can be used by data scientists or social scientists working with geocoded UK data. A common task when working with such data is aggregating data at different administrative levels, e.g. turning postcode-level data into counties or regions. This package can help in achieving this goal and in many other cases involving geospatial data.
Installation
The package can be installed from CRAN with
install.packages("PostcodesioR")
or from GitHub
devtools::install_github("erzk/PostcodesioR")
Once the package is installed, load it with library(PostcodesioR)
Examples
The workhorse of the package is the postcode_lookup()
function which takes a postcode and returns a data frame with the following fields:
postcode
Postcode. All current (‘live’) postcodes within the United Kingdom, the Channel Islands and the Isle of Man, received monthly from Royal Mail. 2, 3 or 4-character outward code, single space and 3-character inward code.quality
Positional Quality. Shows the status of the assigned grid reference.eastings
Eastings. The Ordnance Survey postcode grid reference Easting to 1 metre resolution; blank for postcodes in the Channel Islands and the Isle of Man. Grid references for postcodes in Northern Ireland relate to the Irish Grid system.northings
Northings. The Ordnance Survey postcode grid reference Easting to 1 metre resolution; blank for postcodes in the Channel Islands and the Isle of Man. Grid references for postcodes in Northern Ireland relate to the Irish Grid system.country
Country. The country (i.e. one of the four constituent countries of the United Kingdom or the Channel Islands or the Isle of Man) to which each postcode is assigned.nhs_ha
Strategic Health Authority. The health area code for the postcode.longitude
Longitude. The WGS84 longitude given the Postcode’s national grid reference.latitude
Latitude. The WGS84 latitude given the Postcode’s national grid reference.european_electoral_region
European Electoral Region (EER). The European Electoral Region code for each postcode.primary_care_trust
Primary Care Trust (PCT). The code for the Primary Care areas in England, LHBs in Wales, CHPs in Scotland, LCG in Northern Ireland and PHD in the Isle of Man; there are no equivalent areas in the Channel Islands. Care Trust/ Care Trust Plus (CT) / local health board (LHB) / community health partnership (CHP) / local commissioning group (LCG) / primary healthcare directorate (PHD).region
Region (formerly GOR). The Region code for each postcode. The nine GORs were abolished on 1 April 2024 and are now known as ‘Regions’. They were the primary statistical subdivisions of England and also the areas in which the Government Offices for the Regions fulfilled their role. Each GOR covered a number of local authorities.lsoa
2011 Census lower layer super output area (LSOA). The 2011 Census lower layer SOA code for England and Wales, SOA code for Northern Ireland and data zone code for Scotland.msoa
2011 Census middle layer super output area (MSOA). The 2011 Census middle layer SOA (MSOA) code for England and Wales and intermediate zone for Scotland.incode
Incode. 3-character inward code that is following the space in the full postcode.outcode
Outcode. 2, 3 or 4-character outward code. The part of postcode before the space.parliamentary_constituency
Westminster Parliamentary Constituency. The Westminster Parliamentary Constituency code for each postcode.admin_district
District. The current district/unitary authority to which the postcode has been assigned.parish
Parish (England)/ community (Wales). The smallest type of administrative area in England is the parish (also known as ‘civil parish’); the equivalent units in Wales are communities.admin_county
County. The current county to which the postcode has been assigned.admin_ward
Ward. The current administrative/electoral area to which the postcode has been assigned.ccg
Clinical Commissioning Group. Clinical commissioning groups (CCGs) are NHS organisations set up by the Health and Social Care Act 2012 to organise the delivery of NHS services in England.nuts
Nomenclature of Units for Territorial Statistics (NUTS) / Local Administrative Units (LAU) areas. The LAU2 code for each postcode. NUTS is a hierarchical classification of spatial units that provides a breakdown of the European Union’s territory for producing regional statistics which are comparable across the Union. The NUTS area classification in the United Kingdom comprises current national administrative and electoral areas, except in Scotland where some NUTS areas comprise whole and/or part Local Enterprise Regions. NUTS levels 1-3 are frozen for a minimum of three years and NUTS levels 4 and 5 are now Local Administrative Units (LAU) levels 1 and 2 respectively._code
Returns an ID or Code associated with the postcode. Typically these are a 9 character code known as an ONS Code or GSS Code. This is currently only available for districts, parishes, counties, CCGs, NUTS and wards.
One postcode can be geocoded in the following way
rss <- postcode_lookup("EC1Y8LX")
More than one postcode can be geocoded using purrr
postcodes <- c("EC1Y8LX", "SW1X 7XL")
postcodes_df <- purrr::map_df(postcodes, postcode_lookup)
The remaining functions are demonstrated in the vignette.
Documentation and participation
To read the full documentation of the PostcodesioR package, you can follow this link to the pkgdown site.
If you want to help with developing the package, report bugs or propose pull requests, you will find the GitHub page here.