0

Ultimately I want to use postcodes for all state-funded secondary schools in England, but for now I'm trying to figure out what code I will need to use, so using a selection of just 5.

I want to retrieve the coordinates (so latitude and longitude) and the lsoa value for each postcode.

pc_list <- list(postcodes = c("PE7 3BY", "ME15 9AZ", "BS21 6AH", "SG18 8JB", "M11 2NA"))

pclist1 <- bulk_postcode_lookup(pc_list)

This returns all the information about those 5 postcodes. Now I want it just to return information on those 3 variables (latitude, longitude and lsoa) that I'm interested in.

pclist2 <- subset(pclist1, select = c(longitude, latitude, lsoa))

This returns the following error. Error in subset.default(pclist1, select = c(longitude, latitude, lsoa)) : argument "subset" is missing, with no default

Once I am able to get this information, I want to add these 3 variables along with their relevant postcode into a new dataframe that I can perform susbequent analysis on - is this what pclist2 will be?

Jess
  • 11
  • 2

1 Answers1

0

Slightly modified example from https://docs.ropensci.org/PostcodesioR/articles/Introduction.html#multiple-postcodes , for whatever reason I only received positive responses when removed spaces from postcodes :

library(PostcodesioR)
library(purrr)
pc_list <- list(postcodes = c("PE73BY", "ME159AZ", "BS216AH", "SG188JB", "M112NA"))
pclist1 <- bulk_postcode_lookup(pc_list)
# extract 2nd list item from each response (the "result" list)
bulk_list <- lapply(pclist1, "[[", 2)
# extract list of items from results lists, return tibble / data frame
bulk_df <- map_dfr(bulk_list, `[`, c("postcode", "longitude", "latitude", "lsoa"))

Resulting tibble / data frame :

bulk_df
#> # A tibble: 5 × 4
#>   postcode longitude latitude lsoa                     
#>   <chr>        <dbl>    <dbl> <chr>                    
#> 1 PE7 3BY     -0.226     52.5 Peterborough 019D        
#> 2 ME15 9AZ     0.538     51.3 Maidstone 013C           
#> 3 BS21 6AH    -2.84      51.4 North Somerset 005A      
#> 4 SG18 8JB    -0.249     52.1 Central Bedfordshire 006C
#> 5 M11 2NA     -2.18      53.5 Manchester 015E

Created on 2023-01-13 with reprex v2.0.2

margusl
  • 7,804
  • 2
  • 16
  • 20
  • This is really helpful, thank you. Unfortunately on this last line I'm getting an error when I put it into my R console - do you know what I've done wrong here? `bulk_df <- map_dfr(bulk_list, '[', c("postcode, "longitude", "latitude", "lsoa""))' 'Error: unexpected symbol in "bulk_df <- map_dfr(bulk_list, '[', c("postcode, "longitude"` (note that I used backticks in my actual code around the square bracket) – Jess Jan 14 '23 at 11:32
  • Hard to guess, sorry. Copy-paste just works for me in RStudio 2022.12.0 & R 4.2.2 (in console, in editor and while sourcing saved script). No issues in Google Colab (R 4.2.2) either - https://gist.github.com/marguslt/2b2951cab677dcc447acee1aa0aebbf8 . – margusl Jan 14 '23 at 12:21