hrecht / censusapi

R package to retrieve U.S. Census data and metadata via API
https://www.hrecht.com/censusapi/
169 stars 31 forks source link

error: unknown/unsupported geography heirarchy when querying data for all ZCTA5s #35

Closed chrismp closed 6 years ago

chrismp commented 6 years ago

Hey there, the following code...

getCensus(
     name = "sf1",
     vars = c("P0010001"),
     region = "zip code tabulation area:*",
     vintage = 2010,
     key = Sys.getenv("CENSUS_KEY")
 )

...throws this error... Error: error: unknown/unsupported geography heirarchy.

Any idea what I need to fix here so I can get 2010 population data for every ZCTA5? Replacing "zip code tabulation area:*" with `"county:*" works fine, by the way...

hrecht commented 6 years ago

Hi, the 2010 Decennial SF1 API supports ZCTAs as a region within a state. So, adding a regionin argument will make your code work.

You can see this API's geography hierarchy by running: geos <- listCensusMetadata(name = "sf1", vintage = 2010, type = "geographies") This table details that state is required for zip code tabulation area. (It would be great if this were all written up in the Census documentation too, but the metadata is the best place to look for now.)

So with that, run this for your state of choice:

getCensus(
    name = "sf1",
    vars = c("P0010001"),
    region = "zip code tabulation area:*", 
    regionin="state:4",
    vintage = 2010,
)

Also - the key argument defaults to Sys.getenv("CENSUS_KEY"), so you can omit that argument if you prefer :)