zipcodeR icon indicating copy to clipboard operation
zipcodeR copied to clipboard

Add fips codes to `reverse_zipcode()` output?

Open clauswilke opened this issue 3 years ago • 4 comments

I'm interested in linking zip codes back to counties. It looks like reverse_zipcode() can in principle provide the data I want. However, it only lists county names, not fips codes, as far as I can tell. Would it be possible to add fips codes? It always makes me uncomfortable working with full county names because of the possibility of misspelling or alternative spellings.

A simple translation table from county names to fips codes is available here: https://www.nrcs.usda.gov/wps/portal/nrcs/detail/national/home/?cid=nrcs143_013697

clauswilke avatar Apr 02 '21 15:04 clauswilke

Actually, the table I provided is not sufficient for fips codes. There are locations that are valid but not in this table. For example:

library(zipcodeR)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
#> Loading required package: stringr
reverse_zipcode("22311")
#> 1 row of data found for ZIP code: '22311'
#> # A tibble: 1 x 24
#>   zipcode zipcode_type major_city post_office_city common_city_list county state
#>   <chr>   <chr>        <chr>      <chr>                 <list<raw>> <chr>  <chr>
#> 1 22311   Standard     Alexandria Alexandria, VA               [22] Alexa… VA   
#> # … with 17 more variables: lat <dbl>, lng <dbl>, timezone <chr>,
#> #   radius_in_miles <dbl>, area_code_list <list<raw>>, population <int>,
#> #   population_density <dbl>, land_area_in_sqmi <dbl>,
#> #   water_area_in_sqmi <dbl>, housing_units <int>,
#> #   occupied_housing_units <int>, median_home_value <int>,
#> #   median_household_income <int>, bounds_west <dbl>, bounds_east <dbl>,
#> #   bounds_north <dbl>, bounds_south <dbl>
reverse_zipcode("22311")$county
#> 1 row of data found for ZIP code: '22311'
#> [1] "Alexandria city"

Created on 2021-04-04 by the reprex package (v1.0.0)

The output is correct, as Alexandria City is not in a county. There is a fips code, though. I believe it should be 51510.

clauswilke avatar Apr 04 '21 22:04 clauswilke

Hello Claus! Thanks for the suggestion and your other issues - I appreciate your comments. I'll take a look at these as I work on finishing out the next round of updates.

gavinrozzi avatar Apr 05 '21 01:04 gavinrozzi

Super, thanks!

clauswilke avatar Apr 05 '21 06:04 clauswilke

I have spent some time matching up zip codes with FIPS codes and manually cleaning up any inconsistencies. The result is not perfect, but it works quite well on a very large real-world dataset I'm dealing with.

The final data table is here: https://github.com/clauswilke/zipcodes/blob/main/data/zip2fips.csv

And the code I wrote to generate it can be found here: https://github.com/clauswilke/zipcodes (Specifically, zipcodes.Rmd)

clauswilke avatar Apr 12 '21 22:04 clauswilke