Closed RoyalTS closed 6 years ago
I've done some checking this morning and it appears that the variable PCO0030001
is not available at the Census tract level; httr::GET()
returns a status code of 204
for "no content".
This has been a long-standing issue in tidycensus that I've been unsure how to satisfactorily resolve, as I haven't found a lookup table that states which variables are available (or unavailable) at which geographies. Improving error-handling has been a long-term goal of mine for the package, so perhaps a 204
status code could trigger a suggestion that the variable isn't available for the requested geography.
Yeah, it'd be great in general if the package threw more informative error messages instead of the httr
errors as I often have no idea what to do with those.
Have you talked to the people at the Census directly about such a lookup table? That'd be extremely useful to have!
I've spent the better part of the day learning this the hard way while developing my sociome package. Glad I saw this thread.
Looks like there are variables and geographies listed here: https://api.census.gov/data.html
Give that a look, and if that doesn't provide the necessary info, let's ping Logan Powell for help.
I've reached a semi-resolution to this in https://github.com/walkerke/tidycensus/commit/cb7f542f344a6e329a44898951c57ab12e268783. Errors now return the Census error messages rather than vague R error messages. I think I can iterate and improve on this in the future but this is enough to close the issue.
For example:
tidycensus::get_decennial(geography = 'tract',
variables = c('H0050001', 'PCO0030001'),
year = 2010, county = '001', state = '01')
Getting data from the 2010 decennial Census
Error : One or more of your requested variables is likely not available at the requested geography. Please refine your selection.
It seems like this error may not be due solely to lack of availability at a given census unit. For example, the following throws the same error:
maBlkGrp2010 <- tidycensus::get_decennial(geography = "block group", year = 2010, variables = c(totalPop = "P009001", White = "P009005", Black = "P009006", Asian = "P009008", Hispanic = "P009002"), state = "MA")
Error : One or more of your requested variables is likely not available at the requested geography. Please refine your selection. Error in gather.default(., key = variable, value = value, -GEOID, -NAME) : object 'NAME' not found
BUT those variables are indeed available at block group level using American Fact Finder's Download Center:
It could be that the API has a different set of available geographies, but I don't know how to verify this.
Thanks for the note @profLuna. The issue is with recent changes to the decennial Census API. The old decennial Census endpoint made block group data available by county; I then have internal logic to iterate through all counties in a state and stitch together a state-level block group dataset.
The new endpoint, however, only appears to make block group data available by Census tract for the decennial Census (you can still get it by county for the ACS). Tidycensus can't handle this at the moment.
@loganpowell we ran into a similar issue with state-level Census tract pulls, which the API team fixed. Do you think they would be willing to restore block group pulls by county for the decennial API?
Was this ever solved? I'm running into issues with:
get_decennial(year=2010,geography = "block", variables = c(pop="P001001"), product="population", state = "RI",show_call = TRUE,geometry=TRUE)
Yielding:
Getting data from the 2010 decennial Census Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set
options(tigris_use_cache = TRUE). Using FIPS code '44' for state 'RI' Census API call: https://api.census.gov/data/2010/dec/sf1?get=P001001%2CNAME&for=block%3A%2A&in=state%3A44 Error : Your API call has errors. The API message returned is error: unknown/unsupported geography heirarchy. Error in gather.default(., key = variable, value = value, -GEOID, -NAME) : object 'NAME' not found
even though the block-level data is available.
Thanks, @chihayakenji! I just opened #316 to address this. Will hopefully get around to implementing this in the next week or two now that the API can provide all blocks or block groups in a state with wildcards.
I've hit on a bit of a weird thing where
get_decennial()
will error, returnNA
for some variables or return the correct set of values depending on what combination of variables are being pulled. Minimal-ish reproducible example:yields
,
yields
,
yields
sessionInfo():