|
1 | | -## Pharmacies in Trafford ## |
| 1 | +## Pharmacies in Trafford and Greater Manchester ## |
| 2 | + |
| 3 | +# Source: NHS Business Services Authority (NHSBSA) Master Data Replacement (MDR) |
| 4 | +# Publisher URL: https://opendata.nhsbsa.net/dataset/consolidated-pharmaceutical-list |
| 5 | +# - Consolidated Pharmaceutical List - 2024-25 Quarter 4 |
2 | 6 |
|
3 | | -# Source: Trafford Council |
4 | | -# Publisher URL: http://www.trafford.gov.uk/about-your-council/children-families-and-wellbeing/children-families-and-wellbeing.aspx |
5 | 7 | # Licence: Open Government Licence |
6 | 8 |
|
7 | | -# load libraries |
| 9 | +# Load required packages --------------------------- |
8 | 10 | library(tidyverse) ; library(sf) |
9 | 11 |
|
10 | | -# load data --------------------------- |
11 | | -df <- read_csv("trafford_pharmacies.csv") |
12 | 12 |
|
13 | | -# load local authorities --------------------------- |
14 | | -la <- st_read("https://github.com/traffordDataLab/spatial_data/raw/master/local_authority/2016/gm_local_authority_full_resolution.geojson") %>% |
15 | | - select(area_code, area_name) |
| 13 | +# Load raw data and clean --------------------------- |
| 14 | +df_raw <- read_csv("https://opendata.nhsbsa.net/dataset/240d142d-df82-4e97-b051-12371519e4e1/resource/545609ba-8f93-4adf-8f5a-7a34a1a31881/download/consol_pharmacy_list_202425q4.csv") |
16 | 15 |
|
17 | | -# match with ONS postcodes --------------------------- |
18 | | -postcodes <- read_csv("https://www.traffordDataLab.io/spatial_data/postcodes/GM_postcodes_2018-02.csv") |
19 | | -df_postcodes <- left_join(df, postcodes, by = "postcode") |
| 16 | +df <- df_raw %>% |
| 17 | + filter(HEALTH_AND_WELLBEING_BOARD %in% c("BOLTON","BURY","MANCHESTER","OLDHAM","ROCHDALE","SALFORD","STOCKPORT","TAMESIDE","TRAFFORD","WIGAN")) %>% |
| 18 | + mutate(address = str_to_title(str_replace_all(paste0(ADDRESS_FIELD_1, ", ", ADDRESS_FIELD_2, ", ", ADDRESS_FIELD_3, ", ", ADDRESS_FIELD_4), ", NA", "")), |
| 19 | + HEALTH_AND_WELLBEING_BOARD = str_to_title(HEALTH_AND_WELLBEING_BOARD), |
| 20 | + PHARMACY_TRADING_NAME = str_to_title(PHARMACY_TRADING_NAME), |
| 21 | + ORGANISATION_NAME = str_to_title(ORGANISATION_NAME)) %>% |
| 22 | + arrange(HEALTH_AND_WELLBEING_BOARD, PHARMACY_TRADING_NAME) %>% |
| 23 | + select(pharmacy_name = PHARMACY_TRADING_NAME, |
| 24 | + address, |
| 25 | + postcode = POST_CODE, |
| 26 | + opening_hours_monday = PHARMACY_OPENING_HOURS_MONDAY, |
| 27 | + opening_hours_tuesday = PHARMACY_OPENING_HOURS_TUESDAY, |
| 28 | + opening_hours_wednesday = PHARMACY_OPENING_HOURS_WEDNESDAY, |
| 29 | + opening_hours_thursday = PHARMACY_OPENING_HOURS_THURSDAY, |
| 30 | + opening_hours_friday = PHARMACY_OPENING_HOURS_FRIDAY, |
| 31 | + opening_hours_saturday = PHARMACY_OPENING_HOURS_SATURDAY, |
| 32 | + opening_hours_sunday = PHARMACY_OPENING_HOURS_SUNDAY, |
| 33 | + organisation_data_service_code = `PHARMACY_ODS_CODE_(F-CODE)`, |
| 34 | + health_and_wellbeing_board = HEALTH_AND_WELLBEING_BOARD) |
20 | 35 |
|
21 | | -# convert to spatial object and creat spatial join --------------------------- |
22 | | -sf <- df_postcodes %>% |
23 | | - filter(!is.na(lat)) %>% |
24 | | - st_as_sf(coords = c("long", "lat")) %>% |
25 | | - st_set_crs(4326) %>% |
26 | | - st_join(la, join = st_within, left = FALSE) |
27 | 36 |
|
28 | | -# write data --------------------------- |
29 | | -write_csv(st_set_geometry(sf, value = NULL), "trafford_pharmacies.csv") |
30 | | -st_write(sf, "trafford_pharmacies.geojson") |
| 37 | +# Match pharmacy data with ONS postcodes to obtain spatial coordinates, ward and la information --------------------------- |
| 38 | +postcodes <- read_csv("https://www.trafforddatalab.io/spatial_data/postcodes/gm_postcodes.csv") %>% |
| 39 | + select(postcode, ward_code, ward_name, la_code, la_name, lon, lat) |
| 40 | + |
| 41 | +df_with_coords <- left_join(df, postcodes, by = "postcode") |
31 | 42 |
|
32 | 43 |
|
| 44 | +# Convert to spatial object --------------------------- |
| 45 | +sf <- df_with_coords %>% |
| 46 | + filter(!is.na(lat)) %>% |
| 47 | + st_as_sf(coords = c("lon", "lat")) %>% |
| 48 | + st_set_crs(4326) |
| 49 | + |
33 | 50 |
|
| 51 | +# Write data --------------------------- |
| 52 | +file.remove(c("gm_pharmacies.geojson", |
| 53 | + "trafford_pharmacies.geojson")) |
34 | 54 |
|
| 55 | +# NOTE: There are a few pharmacies where the value of health_and_wellbeing_board differs from la_name, therefore I'm using the former as the filter. |
| 56 | +write_sf(sf, "gm_pharmacies.geojson", driver = "GeoJSON") |
| 57 | +write_sf(sf %>% filter(health_and_wellbeing_board == "Trafford"), "trafford_pharmacies.geojson", driver = "GeoJSON") |
35 | 58 |
|
| 59 | +write_csv(st_set_geometry(sf, value = NULL), "gm_pharmacies.csv") |
| 60 | +write_csv(st_set_geometry(sf, value = NULL) %>% filter(health_and_wellbeing_board == "Trafford"), "trafford_pharmacies.csv") |
0 commit comments