walkerke / tidycensus

Load US Census boundary and attribute data as 'tidyverse' and 'sf'-ready data frames in R
https://walker-data.com/tidycensus
Other
639 stars 100 forks source link

Error subsetting ACS data by ZCTAs #378

Closed anna-nguyen closed 3 years ago

anna-nguyen commented 3 years ago

Hello!

I'm using get_acs to pull ZCTA level characteristics. I want to subset to specific ZCTAs using the zcta option, but run into the following error:

acs_error

When I try to qualify the ZCTA with a state by setting state = "CA", data for all ZIP codes in California is returned.

This seems to be an issue with the Census API itself (I've tried running various API calls directly with no luck :( ), but please let me know if there's a workaround that I might have missed!

walkerke commented 3 years ago

Thanks for the heads up! Looks like Census just changed the API to require a state when requesting ZCTAs by ZCTA. We'll push a fix soon. In the meantime, a workaround that runs quickly:

library(tidycensus)
library(tidyverse)

get_acs(geography = "zcta",
        variables = "C16001_001",
        state = "CA") %>%
  filter(GEOID == "90210")
Getting data from the 2015-2019 5-year ACS
Using FIPS code '06' for state 'CA'
# A tibble: 1 x 5
  GEOID NAME        variable   estimate   moe
  <chr> <chr>       <chr>         <dbl> <dbl>
1 90210 ZCTA5 90210 C16001_001    18656  1010
anna-nguyen commented 3 years ago

Thanks! For my particular use case, I'm pulling data for ZCTAs that span a number of different states. It would be great to make it so multiple ZCTA/state pairs could be passed through as an argument!

walkerke commented 3 years ago

This is now fixed in the master branch:

library(tidycensus)

get_acs(
  geography = "zcta",
  variables = "C16001_001",
  state = "CA",
  zcta = 90210
)
Getting data from the 2015-2019 5-year ACS
Using FIPS code '06' for state 'CA'
# A tibble: 1 x 5
  GEOID NAME        variable   estimate   moe
  <chr> <chr>       <chr>         <dbl> <dbl>
1 90210 ZCTA5 90210 C16001_001    18656  1010

You can also request multiple states & multiple ZCTAs:

get_acs(
  geography = "zcta",
  variables = "C16001_001",
  state = c("CA", "TX"),
  zcta = c(90210, 76109)
)
Getting data from the 2015-2019 5-year ACS
Using FIPS code '06' for state 'CA'
Using FIPS code '48' for state 'TX'
# A tibble: 2 x 5
  GEOID NAME        variable   estimate   moe
  <chr> <chr>       <chr>         <dbl> <dbl>
1 90210 ZCTA5 90210 C16001_001    18656  1010
2 76109 ZCTA5 76109 C16001_001    21808  1108
sisiraul commented 1 year ago

Hi,

I am running into this issue only with both 2016-2020 and 2017-2021 5-year ACS. Is this coming from the Census API? do you have a suggested solution for this? Thank you!

Screenshot 2023-03-15 103136

walkerke commented 1 year ago

@sisiraul As of the 2020 ACS, subsetting by state is no longer supported for ZCTAs on the API side, unfortunately. You could grab a state boundary with tigris and then use that as a filter_by argument in tidycensus; I'll write up an example when I have a minute.

nlinton commented 1 year ago

@walkerke did you ever have a chance to write up that example?

gah-bo commented 5 months ago

@sisiraul As of the 2020 ACS, subsetting by state is no longer supported for ZCTAs on the API side, unfortunately. You could grab a state boundary with tigris and then use that as a filter_by argument in tidycensus; I'll write up an example when I have a minute.

I am wondering why

Are the ZCTA geometry data returned by these two functions different for a given year?

I'm not sure if this is a tidycensus or tigris question, so sorry if this is misplaced.