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

Add option to request a subset of zctas in get_acs() #309

Merged
merged 1 commit into from
Oct 17, 2020

Conversation

mfherman
Copy link
Collaborator

@mfherman mfherman commented Oct 12, 2020

Based on #287, added a new parameter to get_acs() to specify one or more ZCTAs. You can pass a numeric or character vector of ZCTAs. When geometry = TRUE, entire ZCTA shapefiles are pulled from Census, but only selected ZCTAs are included in results.

I added some error messages if a user specifies ZCTAs and geography != "ZCTA", or if using the state/county along with the zcta argument.

I started to work on adding this option to get_decennial() but I realized that there are more geography options with the decennial API so that are you are able to select ZCTAs within states already. Because of this, the GEOIDs used for pulling ZCTA data are state + zip code (e.g. 3610536 rather than just 10536). This makes it a bit trickier to implement the ability to select ZCTAs in the call, but certainly possible. @walkerke: do you think it's worth building in the ZCTA argument for get_decennial(), or is the ability specify ZCTAs with states sufficient?

Here is some code and output with the added feature to get_acs():

get_acs(
   geography = "zcta",
   variables = "B19013_001",
   zcta = c(10536, 11222, 90210)
   )
# Getting data from the 2014-2018 5-year ACS
# A tibble: 3 x 5
#   GEOID NAME        variable   estimate   moe
#   <chr> <chr>       <chr>         <dbl> <dbl>
# 1 10536 ZCTA5 10536 B19013_001   175833 26113
# 2 11222 ZCTA5 11222 B19013_001    85111  3740
# 3 90210 ZCTA5 90210 B19013_001   143542 16099

get_acs(
  geography = "zcta",
  variables = "B19013_001",
  zcta = c("10536", "11222", "90210"),
   geometry = TRUE
  )
# Getting data from the 2014-2018 5-year ACS
# Simple feature collection with 3 features and 5 fields
# geometry type:  MULTIPOLYGON
# dimension:      XY
# bbox:           xmin: -118.4424 ymin: 34.06705 xmax: -73.61156 ymax: 41.31918
# geographic CRS: NAD83
#   GEOID        NAME   variable estimate   moe                       geometry
# 1 90210 ZCTA5 90210 B19013_001   143542 16099 MULTIPOLYGON (((-118.4396 3...
# 2 10536 ZCTA5 10536 B19013_001   175833 26113 MULTIPOLYGON (((-73.62374 4...
# 3 11222 ZCTA5 11222 B19013_001    85111  3740 MULTIPOLYGON (((-73.96248 4...

get_acs(
   state = "NY",
   geography = "zcta",
   variables = "B19013_001",
   zcta = c(10536, 11222)
   )
# Getting data from the 2014-2018 5-year ACS
# Error: ZCTAs can only be requested for the entire country or by specifying ZCTAs, not within states or counties.

get_acs(
   state = "NY",
   geography = "tract",
   variables = "B19013_001",
   zcta = c(10536, 11222)
   )
# Getting data from the 2014-2018 5-year ACS
# Error: ZCTAs can only be specified when requesting data at the zip code tabulation area-level.

@walkerke
Copy link
Owner

Looks great! In theory, this opens up the possibility of requesting data for other geographies that aren't aligned with the core Census hierarchy that we don't currently support in tidycensus - which has long been a goal of mine.

@walkerke walkerke merged commit c2be0f5 into walkerke:master Oct 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants