1

I am trying to utilize the noncensus package in R to join the data("zip_codes") which contains the county level fips code to get the data("counties") that has the actual county name.

My data set contains the zip codes for 100's of observations and I am trying to match up what county they are in. Both counties and zip_codes have the county fips code but when I join them they don't match as I get 0 values returned.

library(noncensus) 
data("zip_codes") 
data("counties") 
counties$county_fips <- as.numeric(as.character(counties$county_fips)) 
Test <- zip_codes %>% 
  left_join(counties, c("fips"="county_fips")) 
Test <- Test %>% 
  slice(1:5) %>% 
  select(zip, city, state.x, county_name)

If there are other packages in R to get the county from a zip code I'd be open to try that as well.

Thanks,

neilfws
  • 32,751
  • 5
  • 50
  • 63
Drthm1456
  • 409
  • 9
  • 17
  • It would help to see some example data, code you have tried and example results or error messages. – neilfws Oct 19 '17 at 21:34
  • library(noncensus) data("zip_codes") data("counties") head(zip_codes) head(counties) counties$county_fips <- as.numeric(as.character(counties$county_fips)) Test <- zip_codes %>% left_join(counties, c("fips"="county_fips")) Test <- Test %>% slice(1:5) %>% select(zip, city, state.x, county_name) – Drthm1456 Oct 19 '17 at 22:06
  • it will return no values for county names – Drthm1456 Oct 19 '17 at 22:07
  • The `fips` in `zip_codes` start at 1001, but the `county_fips` in `counties` go from 001 to 840. So there is no overlap and indeed the numbers used in coding seem different. You'd have to delve into how each dataset was constructed to figure out the codes. – neilfws Oct 19 '17 at 22:20
  • You can try `totalcensus` package to process census data https://github.com/GL-Li/totalcensus. – GL_Li Jan 08 '18 at 00:56
  • The documentation for `zip_codes` in `noncensus` claims that the `fips` field is the "County FIPS Code", which it clearly is not. – steveo'america Apr 05 '19 at 23:59

1 Answers1

1

ZCTA FIPS codes have no relationship to county FIPS codes. ZCTAs also don't nest within counties and can cross county boundaries. As such, you'll need spatial methods without some other correspondence table. This R code will do it:

library(tigris)
library(tidyverse)
library(sf)
options(tigris_use_cache = TRUE)
options(tigris_class = "sf")

cty <- counties(cb = TRUE) %>%
  select(cty_id = GEOID, cty_name = NAME)

zc <- zctas(cb = TRUE)

zipcty <- st_join(zc, cty)

The result returns a row for each unique ZCTA/county combination; the default spatial method for st_join is "intersects" so this could mean that the ZCTA lies within, crosses the boundary of, or touches the boundary of the county.

kwalkertcu
  • 1,011
  • 6
  • 8
  • Interesting, I haven't utilized the Tigris package before. I look forward to diving into this package further. – Drthm1456 Oct 25 '17 at 01:41