--- title: "Getting started with NHSRpopulation" output: rmarkdown::html_vignette link-citations: TRUE vignette: > %\VignetteIndexEntry{Getting started with NHSRpopulation} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ### Indices of Multiple Deprivation (IMD) To get the IMD scores (raw scores and ranked deciles) for a dataset run the following code to generate some random example postcodes: ```r tibble_postcodes <- tibble::tibble(postcode = c("HD1 2UT", "HD1 2UU", "HD1 2UV")) tibble_postcodes #> # A tibble: 3 × 1 #> postcode #> #> 1 HD1 2UT #> 2 HD1 2UU #> 3 HD1 2UV ``` Then, using the `get_data()` function for a vector: ```r # Execution halted NHSRpopulation::get_data(postcodes) |> dplyr::select( new_postcode, result_type, lsoa_code ) #> ℹ 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 #> Error in `dplyr::select()`: #> ! Can't select columns that don't exist. #> ✖ Column `postcode` doesn't exist. ``` Or with a data frame: ```r NHSRpopulation::get_data(tibble_postcodes) |> dplyr::select( postcode, new_postcode, result_type, lsoa_code ) #> ℹ 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 #> Joining with `by = join_by(postcode)` #> # A tibble: 3 × 4 #> postcode new_postcode result_type lsoa_code #> #> 1 HD1 2UT HD1 2RD terminated E01011107 #> 2 HD1 2UU HD1 2UU valid E01011229 #> 3 HD1 2UV HD1 2UD autocompleted E01011229 ``` Note that this function uses the {NHSRpostcodetools} package to offer the opportunity to fix postcodes which are terminated or are incorrect. This is default and appears in the column `new_postcode` and does not overwrite the original `postcode` column. Switching off this automatic fix can be done with the code and will accept both vectors and data frames: ```r NHSRpopulation::get_data(tibble_postcodes, fix_invalid = FALSE) |> dplyr::select( postcode, new_postcode, result_type, lsoa_code ) #> ℹ 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. #> Joining with `by = join_by(postcode)` #> # A tibble: 3 × 4 #> postcode new_postcode result_type lsoa_code #> #> 1 HD1 2UT #> 2 HD1 2UU HD1 2UU valid E01011229 #> 3 HD1 2UV ``` # Index of Multiple Deprivation ```r # Note that the third LSOA in this list is incorrect on purpose imd <- c("E01011107", "E01011229", "E01002") tibble_imd <- imd |> tibble::as_tibble() |> dplyr::rename(lsoa11 = value) tibble_imd #> # A tibble: 3 × 1 #> lsoa11 #> #> 1 E01011107 #> 2 E01011229 #> 3 E01002 ``` Using the same function but with a parameter/argument to return IMD data: ```r NHSRpopulation::get_data(tibble_imd, url_type = "imd") |> dplyr::select( lsoa11, imd_rank, imd_decile, imd_score ) #> # A tibble: 3 × 4 #> lsoa11 imd_rank imd_decile imd_score #> #> 1 E01011107 2928 1 45.6 #> 2 E01011229 9558 3 27.0 #> 3 E01002 NA NA NA ``` Data can be either vectors or data frames. No corrections are made to incorrect LSOA codes. # Column names Where data frames are used the expectation of the functions is that postcodes will be in a column called `postcode` and IMD will be from `lsoa11`, however, this can be overwritten: The argument/parameter `column = ` can be used to set the column name: ```r # Create datasets pcs_tb <- dplyr::tibble( pcs = postcodes ) pcs_tb #> # A tibble: 3 × 1 #> pcs #> #> 1 HD1 2UT #> 2 HD1 2UU #> 3 HD1 2UV NHSRpopulation::get_data(pcs_tb, column = "pcs" ) #> ℹ 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 #> Joining with `by = join_by(pcs)` #> # A tibble: 3 × 40 #> pcs new_postcode result_type quality eastings northings country nhs_ha longitude latitude european_electoral_r…¹ primary_care_trust region lsoa msoa incode #> #> 1 HD1 2UT HD1 2RD terminated 1 414639 416430 England Yorkshir… -1.78 53.6 Yorkshire and The Hum… Kirklees Yorks… Kirk… Kirk… 2RD #> 2 HD1 2UU HD1 2UU valid 1 414433 416422 England Yorkshir… -1.78 53.6 Yorkshire and The Hum… Kirklees Yorks… Kirk… Kirk… 2UU #> 3 HD1 2UV HD1 2UD autocompleted 1 414371 416317 England Yorkshir… -1.78 53.6 Yorkshire and The Hum… Kirklees Yorks… Kirk… Kirk… 2UD #> # ℹ abbreviated name: ¹​european_electoral_region #> # ℹ 24 more variables: outcode , parliamentary_constituency , parliamentary_constituency_2024 , admin_district , parish , #> # date_of_introduction , admin_ward , ccg , nuts , pfa , admin_district_code , admin_county_code , admin_ward_code , #> # parish_code , parliamentary_constituency_code , parliamentary_constituency_2024_code , ccg_code , ccg_id_code , ced_code , #> # nuts_code , lsoa_code , msoa_code , lau2_code , pfa_code ``` ```r lsoa_tb <- dplyr::tibble( lower_soa = imd ) lsoa_tb #> # A tibble: 3 × 1 #> lower_soa #> #> 1 E01011107 #> 2 E01011229 #> 3 E01002 NHSRpopulation::get_data(lsoa_tb, column = "lower_soa" ) #> # A tibble: 3 × 66 #> lower_soa fid lsoa11nm lsoa11nmw st_areasha st_lengths imd_rank imd_decile lsoa01nm la_dcd la_dnm imd_score imd_rank0 imd_dec0 inc_score inc_rank inc_dec emp_score #> #> 1 E01011107 11200 Kirklees… Kirklees… 1921709. 7525. 2928 1 Kirklee… E0800… Kirkl… 45.6 2928 1 0.256 3753 2 0.176 #> 2 E01011229 11707 Kirklees… Kirklees… 833130. 7023. 9558 3 Kirklee… E0800… Kirkl… 27.0 9558 3 0.092 17587 6 0.068 #> 3 E01002 NA NA NA NA NA NA NA NA NA NA NA NA #> # ℹ 48 more variables: emp_rank , emp_dec , edu_score , edu_rank , edu_dec , hdd_score , hdd_rank , hdd_dec , cri_score , #> # cri_rank , cri_dec , bhs_score , bhs_rank , bhs_dec , env_score , env_rank , env_dec , idc_score , idc_rank , #> # idc_dec , ido_score , ido_rank , ido_dec , cyp_score , cyp_rank , cyp_dec , as_score , as_rank , as_dec , #> # gb_score , gb_rank , gb_dec , wb_score , wb_rank , wb_dec , ind_score , ind_rank , ind_dec , out_score , #> # out_rank , out_dec , tot_pop , dep_chi , pop16_59 , pop60 , work_pop , shape_area , shape_length ``` ## Getting IMD data from postcodes If the data has postcodes (which automatically connects to the postcode API) and IMD information is wanted, the argument/parameter `url_type == "imd"` will override the returned data to IMD. ```r NHSRpopulation::get_data(tibble_postcodes, url_type = "imd") #> ℹ 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 #> Joining with `by = join_by(postcode)` #> # A tibble: 3 × 105 #> postcode new_postcode result_type quality eastings northings country nhs_ha longitude latitude european_electoral_r…¹ primary_care_trust region lsoa msoa incode #> #> 1 HD1 2UT HD1 2RD terminated 1 414639 416430 England Yorkshi… -1.78 53.6 Yorkshire and The Hum… Kirklees Yorks… Kirk… Kirk… 2RD #> 2 HD1 2UU HD1 2UU valid 1 414433 416422 England Yorkshi… -1.78 53.6 Yorkshire and The Hum… Kirklees Yorks… Kirk… Kirk… 2UU #> 3 HD1 2UV HD1 2UD autocompleted 1 414371 416317 England Yorkshi… -1.78 53.6 Yorkshire and The Hum… Kirklees Yorks… Kirk… Kirk… 2UD #> # ℹ abbreviated name: ¹​european_electoral_region #> # ℹ 89 more variables: outcode , parliamentary_constituency , parliamentary_constituency_2024 , admin_district , parish , #> # date_of_introduction , admin_ward , ccg , nuts , pfa , admin_district_code , admin_county_code , admin_ward_code , #> # parish_code , parliamentary_constituency_code , parliamentary_constituency_2024_code , ccg_code , ccg_id_code , ced_code , #> # nuts_code , lsoa_code , msoa_code , lau2_code , pfa_code , fid , lsoa11nm , lsoa11nmw , st_areasha , st_lengths , #> # imd_rank , imd_decile , lsoa01nm , la_dcd , la_dnm , imd_score , imd_rank0 , imd_dec0 , inc_score , inc_rank , #> # inc_dec , emp_score , emp_rank , emp_dec , edu_score , edu_rank , edu_dec , hdd_score , hdd_rank , hdd_dec , … ``` Note that the postcode data is still validated. ```r NHSRpopulation::get_data(tibble_postcodes, url_type = "imd", 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. #> Joining with `by = join_by(postcode)` #> # A tibble: 3 × 105 #> postcode new_postcode result_type quality eastings northings country nhs_ha longitude latitude european_electoral_r…¹ primary_care_trust region lsoa msoa incode #> #> 1 HD1 2UT NA NA NA NA NA #> 2 HD1 2UU HD1 2UU valid 1 414433 416422 England Yorkshire… -1.78 53.6 Yorkshire and The Hum… Kirklees Yorks… Kirk… Kirk… 2UU #> 3 HD1 2UV NA NA NA NA NA #> # ℹ abbreviated name: ¹​european_electoral_region #> # ℹ 89 more variables: outcode , parliamentary_constituency , parliamentary_constituency_2024 , admin_district , parish , #> # date_of_introduction , admin_ward , ccg , nuts , pfa , admin_district_code , admin_county_code , admin_ward_code , #> # parish_code , parliamentary_constituency_code , parliamentary_constituency_2024_code , ccg_code , ccg_id_code , ced_code , #> # nuts_code , lsoa_code , msoa_code , lau2_code , pfa_code , fid , lsoa11nm , lsoa11nmw , st_areasha , st_lengths , #> # imd_rank , imd_decile , lsoa01nm , la_dcd , la_dnm , imd_score , imd_rank0 , imd_dec0 , inc_score , inc_rank , #> # inc_dec , emp_score , emp_rank , emp_dec , edu_score , edu_rank , edu_dec , hdd_score , hdd_rank , hdd_dec , … ```