Skip to contents

Checking for valid postcodes

Both vectors and tibbles can be passed through the functions. Starting with a vector of postcodes with the first being terminated, the second
is valid and the third is invalid.

A vector of postcodes

# a vector
postcodes <- c("HD1 2UT", "HD1 2UU", "HD1 2UV")

Join this vector to the postcode data

postcode_data_join(postcodes, fix_invalid = TRUE)
#>  The following postcodes are terminated:
#>   HD1 2UT
#>   and have been replaced with these current postcodes:
#>   HD1 2RD
#>  The following postcodes are invalid:
#>   HD1 2UV
#>   and have been replaced with these nearby postcodes:
#>   HD1 2UD
#> # A tibble: 3 × 40
#>   postcode new_postcode result_type   quality eastings northings country nhs_ha 
#>   <chr>    <chr>        <chr>           <int>    <int>     <int> <chr>   <chr>  
#> 1 HD1 2UT  HD1 2RD      terminated          1   414639    416430 England Yorksh…
#> 2 HD1 2UU  HD1 2UU      valid               1   414433    416422 England Yorksh…
#> 3 HD1 2UV  HD1 2UD      autocompleted       1   414371    416317 England Yorksh…
#> # ℹ 32 more variables: longitude <dbl>, latitude <dbl>,
#> #   european_electoral_region <chr>, primary_care_trust <chr>, region <chr>,
#> #   lsoa <chr>, msoa <chr>, incode <chr>, outcode <chr>,
#> #   parliamentary_constituency <chr>, parliamentary_constituency_2024 <chr>,
#> #   admin_district <chr>, parish <chr>, date_of_introduction <chr>,
#> #   admin_ward <chr>, ccg <chr>, nuts <chr>, pfa <chr>,
#> #   admin_district_code <chr>, admin_county_code <chr>, …

A tibble of postcodes

test_df1 <- dplyr::tibble(
  place = paste0("place_", seq(3L)),
  postcode = postcodes
)

For a tibble or data frame the code to extract to pass through the functions in this package the postcode column needs to be recoded to postcodes in order to be recognised.

postcode_data_join(test_df1, fix_invalid = TRUE)
#>  The following postcodes are terminated:
#>   HD1 2UT
#>   and have been replaced with these current postcodes:
#>   HD1 2RD
#>  The following postcodes are invalid:
#>   HD1 2UV
#>   and have been replaced with these nearby postcodes:
#>   HD1 2UD
#> # A tibble: 3 × 41
#>   place   postcode new_postcode result_type   quality eastings northings country
#>   <chr>   <chr>    <chr>        <chr>           <int>    <int>     <int> <chr>  
#> 1 place_1 HD1 2UT  HD1 2RD      terminated          1   414639    416430 England
#> 2 place_2 HD1 2UU  HD1 2UU      valid               1   414433    416422 England
#> 3 place_3 HD1 2UV  HD1 2UD      autocompleted       1   414371    416317 England
#> # ℹ 33 more variables: nhs_ha <chr>, longitude <dbl>, latitude <dbl>,
#> #   european_electoral_region <chr>, primary_care_trust <chr>, region <chr>,
#> #   lsoa <chr>, msoa <chr>, incode <chr>, outcode <chr>,
#> #   parliamentary_constituency <chr>, parliamentary_constituency_2024 <chr>,
#> #   admin_district <chr>, parish <chr>, date_of_introduction <chr>,
#> #   admin_ward <chr>, ccg <chr>, nuts <chr>, pfa <chr>,
#> #   admin_district_code <chr>, admin_county_code <chr>, …

Note that the parameter fix_invalid = TRUE defaults to TRUE:

postcode_data_join(test_df1)
#>  The following postcodes are terminated:
#>   HD1 2UT
#>   and have been replaced with these current postcodes:
#>   HD1 2RD
#>  The following postcodes are invalid:
#>   HD1 2UV
#>   and have been replaced with these nearby postcodes:
#>   HD1 2UD
#> # A tibble: 3 × 41
#>   place   postcode new_postcode result_type   quality eastings northings country
#>   <chr>   <chr>    <chr>        <chr>           <int>    <int>     <int> <chr>  
#> 1 place_1 HD1 2UT  HD1 2RD      terminated          1   414639    416430 England
#> 2 place_2 HD1 2UU  HD1 2UU      valid               1   414433    416422 England
#> 3 place_3 HD1 2UV  HD1 2UD      autocompleted       1   414371    416317 England
#> # ℹ 33 more variables: nhs_ha <chr>, longitude <dbl>, latitude <dbl>,
#> #   european_electoral_region <chr>, primary_care_trust <chr>, region <chr>,
#> #   lsoa <chr>, msoa <chr>, incode <chr>, outcode <chr>,
#> #   parliamentary_constituency <chr>, parliamentary_constituency_2024 <chr>,
#> #   admin_district <chr>, parish <chr>, date_of_introduction <chr>,
#> #   admin_ward <chr>, ccg <chr>, nuts <chr>, pfa <chr>,
#> #   admin_district_code <chr>, admin_county_code <chr>, …

And if it is set to FALSE the same message appears but the new_postcode is not populated and has NA.

postcode_data_join(test_df1, fix_invalid = FALSE)
#>  The following postcodes are invalid:
#>   HD1 2UT
#>   but have not been successfully replaced with valid codes.
#>   The following postcodes are invalid:
#>   HD1 2UV
#>   but have not been successfully replaced with valid codes.
#> # A tibble: 3 × 41
#>   place   postcode new_postcode result_type quality eastings northings country
#>   <chr>   <chr>    <chr>        <chr>         <int>    <int>     <int> <chr>  
#> 1 place_1 HD1 2UT  NA           NA               NA       NA        NA NA     
#> 2 place_2 HD1 2UU  HD1 2UU      valid             1   414433    416422 England
#> 3 place_3 HD1 2UV  NA           NA               NA       NA        NA NA     
#> # ℹ 33 more variables: nhs_ha <chr>, longitude <dbl>, latitude <dbl>,
#> #   european_electoral_region <chr>, primary_care_trust <chr>, region <chr>,
#> #   lsoa <chr>, msoa <chr>, incode <chr>, outcode <chr>,
#> #   parliamentary_constituency <chr>, parliamentary_constituency_2024 <chr>,
#> #   admin_district <chr>, parish <chr>, date_of_introduction <chr>,
#> #   admin_ward <chr>, ccg <chr>, nuts <chr>, pfa <chr>,
#> #   admin_district_code <chr>, admin_county_code <chr>, …