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

Getting acs subject tables #46

Closed arilamstein closed 6 years ago

arilamstein commented 6 years ago

Does censusapi support getting acs subject tables? For example, I have a client who would like to import all data in table S1902 for the state of Michigan into R. You can see the data in American Fact Finder here: https://factfinder.census.gov/faces/tableservices/jsf/pages/productview.xhtml?pid=ACS_16_5YR_S2703&prodType=table.

I wasn't sure if censusapi supports importing subject tables. I wrote this code, attempting to import all the data in the above AFF page into R:

getCensus(name = "acs/acs5",
          vintage = 2016, 
          vars = "S1902", 
          region = "state:26")

I got this error

Error in apiCheck(req) : 
  The Census Bureau returned the following error message:
 error: error: unknown variable 'S1902'

Any advice would be appreciated.

hrecht commented 6 years ago

Yes, censusapi supports the ACS subject tables. There are two issues with the code above - you need to use the correct endpoint name and retrieve a valid list of vars and/or a variable group. The endpoint names can be found by running apis <- listCensusApis() and searching for the ACS subject tables - in this case you want acs/acs5/subject.

S1902 is a group/table name, not a variable name. You can retrieve that group of variables in censusapi, but need to specify that it is a group. The full code is:

getCensus(name = "acs/acs5/subject",
  vintage = 2016, 
  vars = "group(S1902)", 
  region = "state:26")

I recommend checking out the intro tutorial for how to find a dataset name and variable metadata.