diff --git a/getBoundaryData.R b/R/getBoundaryData.R similarity index 100% rename from getBoundaryData.R rename to R/getBoundaryData.R diff --git a/R/postcodeWrangling.R b/R/postcodeWrangling.R new file mode 100644 index 0000000000000000000000000000000000000000..b14ad12e0c8393bc0b5c8705e998544889089280 --- /dev/null +++ b/R/postcodeWrangling.R @@ -0,0 +1,34 @@ +# postcodeWrangling.R: doing stuff with postcodes +library(data.table) # fast + +dp <- path.expand("~/Dropbox/data/UK_postcodes/") # where you keep your postcode data + +# load GOR region names ---- +df <- "NSPL_AUG_2020_UK/Documents/Region names and codes EN as at 12_10 (GOR).xlsx" +region_codes <- readxl::read_xlsx(paste0(dp,df)) +region_codes_dt <- data.table::as.data.table(region_codes) +data.table::setkey(region_codes_dt, GOR10CD) + +# derive postcode sectors & add GOR region names ---- + +# > 2016 ---- +df <- "~/Dropbox/data/UK_postcodes/NSPL_AUG_2016_UK_V2/Data/NSPL_AUG_2016_UK.csv.gz" +rawDT <- data.table::fread(df) +rawDT[, pcd_district := data.table::tstrsplit(pcds, " ", keep = c(1))] # the characters before the space are the postcode district +# should be 3088 (UK) + 26 = 3114 (UK + IoM & Channel Islands) +data.table::uniqueN(rawDT$pcd_district) +rawDT[, GOR10CD := gor] +data.table::setkey(rawDT, GOR10CD) +rawDT <- region_codes_dt[rawDT] # match on GOR codes & names +rawDT[osgrdind < 9, .(n = .N), keyby = .(gor,GOR10NM)] +# there are some rows without a GOR10NM (or gor) +head(rawDT[is.na(GOR10NM)]) +pcd_districts_2016_dt <- rawDT[doterm < 201608 & osgrdind < 9, # remove terminated & those without grid references + .(nPostcodes = .N), keyby = .(pcd_district, GOR10CD, GOR10NM)] +data.table::uniqueN(pcd_districts_2016_dt$pcd_district) +skimr::skim(pcd_districts_2016_dt) +# all postcode districts should now match to a GOR +table(pcd_districts_2016_dt$GOR10CD,pcd_districts_2016_dt$GOR10NM, useNA = "always") + +# save the file for future use +data.table::fwrite(pcd_districts_2016_dt, file = paste0(dp, "postcode_districts_2016.csv")) diff --git a/README.md b/README.md index 4fd4918a80caf0ea029799bc97fab03688a61b58..917d815cf1c605928b4a0b68f6c43ce1e84ff98f 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,19 @@ You link these using the relevant area level codes such as: ### rMappingExample -A [simple example of mapping using R](rMappingExample.html). Examples use Local Authority (LA) and Middle-layer Super Output Area (MSOA) geography and electricity consumption data (provided in `data` subfolder). +A [simple example of mapping using R](rMappingExample/rMappingExample.html). Examples using Local Authority (LA) and Middle-layer Super Output Area (MSOA) geography and electricity consumption data (provided in `data` subfolder). ### ONS-open-geography -An [example and resources](ONS-open-geography.html) for using the [ONS Open Geography portal](https://geoportal.statistics.gov.uk) to download geographical data and create maps using `Leaflet` package. +An [example and resources](onsOpenGeography/ONS-open-geography.html) for using the [ONS Open Geography portal](https://geoportal.statistics.gov.uk) to download geographical data and create maps using `Leaflet` package. -### fun with cartograms +See the [open-geog-API](open-geog-API) folder for API examples + +### LSOA example + +What it says [on the tin](/lsoaExample/lsoaExample.html) + +### Fun with cartograms What it says [on the tin](/cartograms/hex-cartograms.html) diff --git a/cartograms/hex-cartograms.R b/funWithCartograms/hex-cartograms.R similarity index 100% rename from cartograms/hex-cartograms.R rename to funWithCartograms/hex-cartograms.R diff --git a/cartograms/hex-cartograms.Rmd b/funWithCartograms/hex-cartograms.Rmd similarity index 100% rename from cartograms/hex-cartograms.Rmd rename to funWithCartograms/hex-cartograms.Rmd diff --git a/cartograms/hex-cartograms.html b/funWithCartograms/hex-cartograms.html similarity index 100% rename from cartograms/hex-cartograms.html rename to funWithCartograms/hex-cartograms.html diff --git a/lsoaExample.Rmd b/lsoaExample/lsoaExample.Rmd similarity index 100% rename from lsoaExample.Rmd rename to lsoaExample/lsoaExample.Rmd diff --git a/lsoaExample.html b/lsoaExample/lsoaExample.html similarity index 100% rename from lsoaExample.html rename to lsoaExample/lsoaExample.html diff --git a/ONS-open-geography.Rmd b/onsOpenGeography/ONS-open-geography.Rmd similarity index 100% rename from ONS-open-geography.Rmd rename to onsOpenGeography/ONS-open-geography.Rmd diff --git a/ONS-open-geography.html b/onsOpenGeography/ONS-open-geography.html similarity index 100% rename from ONS-open-geography.html rename to onsOpenGeography/ONS-open-geography.html diff --git a/rMappingExample.Rmd b/rMappingExample/rMappingExample.Rmd similarity index 100% rename from rMappingExample.Rmd rename to rMappingExample/rMappingExample.Rmd diff --git a/rMappingExample.html b/rMappingExample/rMappingExample.html similarity index 100% rename from rMappingExample.html rename to rMappingExample/rMappingExample.html diff --git a/rMappingExample.pdf b/rMappingExample/rMappingExample.pdf similarity index 100% rename from rMappingExample.pdf rename to rMappingExample/rMappingExample.pdf