Created
September 26, 2025 23:28
-
-
Save andrewheiss/90525f5e0c0b44ce0ce88b9b1192317d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(tidyverse) | |
| # Here's some state-level data | |
| some_state_data <- tribble( | |
| ~state, ~something, | |
| "Wyoming", 5, | |
| "North Carolina", 9, | |
| "Nevada", 10, | |
| "Georgia", 3, | |
| "Rhode Island", 1, | |
| "District of Columbia", 6 | |
| ) | |
| # Neat, but it would be nice to know what divisons/regions these are in! | |
| some_state_data | |
| #> # A tibble: 6 × 2 | |
| #> state something | |
| #> <chr> <dbl> | |
| #> 1 Wyoming 5 | |
| #> 2 North Carolina 9 | |
| #> 3 Nevada 10 | |
| #> 4 Georgia 3 | |
| #> 5 Rhode Island 1 | |
| #> 6 District of Columbia 6 | |
| # Build a lookup table with {tigris} | |
| library(tigris) | |
| regions_info <- regions() |> | |
| sf::st_drop_geometry() |> | |
| select(REGION_ID = GEOID, REGION = NAME) | |
| divisions_info <- divisions() |> | |
| sf::st_drop_geometry() |> | |
| select(DIVISION_ID = GEOID, DIVISION = NAME) | |
| states_info <- states(cb = FALSE) |> | |
| sf::st_drop_geometry() |> | |
| select(REGION_ID = REGION, DIVISION_ID = DIVISION, STATEFP, STUSPS, NAME) | |
| state_details <- states_info |> | |
| left_join(regions_info, by = join_by(REGION_ID)) |> | |
| left_join(divisions_info, by = join_by(DIVISION_ID)) |> | |
| select( | |
| state = NAME, | |
| state_abb = STUSPS, | |
| state_fips = STATEFP, | |
| state_region = REGION, | |
| state_division = DIVISION | |
| ) |> | |
| as_tibble() | |
| # Complete details! | |
| state_details | |
| #> # A tibble: 56 × 5 | |
| #> state state_abb state_fips state_region state_division | |
| #> <chr> <chr> <chr> <chr> <chr> | |
| #> 1 West Virginia WV 54 South South Atlantic | |
| #> 2 Florida FL 12 South South Atlantic | |
| #> 3 Illinois IL 17 Midwest East North Central | |
| #> 4 Minnesota MN 27 Midwest West North Central | |
| #> 5 Maryland MD 24 South South Atlantic | |
| #> 6 Rhode Island RI 44 Northeast New England | |
| #> 7 Idaho ID 16 West Mountain | |
| #> 8 New Hampshire NH 33 Northeast New England | |
| #> 9 North Carolina NC 37 South South Atlantic | |
| #> 10 Vermont VT 50 Northeast New England | |
| #> # ℹ 46 more rows | |
| # Join this to the original data | |
| some_state_data |> | |
| left_join(state_details, by = join_by(state)) | |
| #> # A tibble: 6 × 6 | |
| #> state something state_abb state_fips state_region state_division | |
| #> <chr> <dbl> <chr> <chr> <chr> <chr> | |
| #> 1 Wyoming 5 WY 56 West Mountain | |
| #> 2 North Carolina 9 NC 37 South South Atlantic | |
| #> 3 Nevada 10 NV 32 West Mountain | |
| #> 4 Georgia 3 GA 13 South South Atlantic | |
| #> 5 Rhode Island 1 RI 44 Northeast New England | |
| #> 6 District of Columb… 6 DC 11 South South Atlantic |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment