Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function: ZIP/FIPS to County #25

Closed
andrewallenbruce opened this issue Oct 26, 2023 · 0 comments
Closed

Function: ZIP/FIPS to County #25

andrewallenbruce opened this issue Oct 26, 2023 · 0 comments
Assignees
Labels
feature 📸 feature request geospatial 🌎 map making , geocoding, etc.
Milestone

Comments

@andrewallenbruce
Copy link
Owner

Example use-case: (#18)

# `{zipcodeR}`: Retrieve Counties from Zip codes 
zip_db <- dplyr::tibble(zip_code_db) |> 
  dplyr::filter(state == "GA" | state == "FL") |> 
  dplyr::select(
    zipcode, 
    city = major_city, 
    county, 
    state, 
    lat, 
    lng, 
    pop_total = population, 
    pop_dense = population_density,
    med_income = median_household_income,
    dplyr::contains("bounds"))

# Join RHCs to Counties
rhc_zip <- dplyr::left_join(
  pivoted, ## zip code would be normalized dplyr::mutate(zipnorm = zipcodeR::normalize_zip(zip))
  zip_db, 
  by = dplyr::join_by(state, zipnorm == zipcode))

# Create data frame of unique state/county pairs
# Remove "County" from county names
st_cnty <- rhc_zip |> 
  distinct(state, county) |> 
  mutate(county = stringr::str_remove(county, " County"))

# Make separate, equal-length lists of the states and counties
x <- list(st_cnty$state)
y <- list(st_cnty$county)

# Retrieve county FIPS codes
# Retrieve geometries based on county FIPS
z    <- purrr::map2(x, y, fipio::as_fips)
z_sf <- purrr::map(z, fipio::fips_geometry) |> purrr::list_rbind()
z_sf[[1]]

# Unlist & insert into new tibble with geometry
st_ct_fips <- dplyr::tibble(
  state = unlist(x),
  county = unlist(y),
  fips = unlist(z),
  geometry = z_sf[[1]])
@andrewallenbruce andrewallenbruce added feature 📸 feature request geospatial 🌎 map making , geocoding, etc. labels Oct 26, 2023
@andrewallenbruce andrewallenbruce added this to the 0.0.1 milestone Oct 26, 2023
@andrewallenbruce andrewallenbruce self-assigned this Oct 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 📸 feature request geospatial 🌎 map making , geocoding, etc.
Projects
None yet
Development

No branches or pull requests

1 participant