hrecht / censusapi

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

Pulling COUSUB in Decennial 2020 response rates #64

Closed sarasatullo closed 4 years ago

sarasatullo commented 4 years ago

Hi, I'm trying to pull the 2020 Census response rates for all of the towns, cities and municipalities in two Pennsylvania counties. I cannot seem to get COUSUB to work even thought it is listed as a geography. Everything I do throws this error: Error in apiCheck(req) : The Census Bureau returned the following error message: error: unknown/unsupported geography heirarchy

library(censusapi)
library(dplyr)

pa_county_responses <- getCensus(
  name = "dec/responserate",
  vintage = 2020,
  vars = c("NAME", "RESP_DATE", "CRRALL", "CRRINT"),
  region = "county:*",
  regionin = "state:42")

#Get variables
response_vars <- listCensusMetadata(
  name = "dec/responserate",
  vintage = 2020,
  type = "variables"
)

#This works but not all of our local municipalities come up under places
lv_responses <- getCensus(
    name = "dec/responserate",
    vintage = 2020,
    vars = c("NAME", "RESP_DATE", "CRRALL", "CRRINT"),
    region = "place:*",
    regionin = `"state:42")`

#API geographic hierarchy says state> county > countysubdivision, so I've tried all of below

  lv_responses <- getCensus(
    name = "dec/responserate",
    vintage = 2020,
    vars = c("NAME", "RESP_DATE", "CRRALL", "CRRINT"),
    region = "county:*",
    regionin = "state:42+cousub:077")

  lv_responses <- getCensus(
    name = "dec/responserate",
    vintage = 2020,
    vars = c("NAME", "RESP_DATE", "CRRALL", "CRRINT"),
        region = "cousub:*",
    regionin = "state:42"
    )

  lv_responses <- getCensus(
    name = "dec/responserate",
    vintage = 2020,
    vars = c("NAME", "RESP_DATE", "CRRALL", "CRRINT"),
    region = "cousub:*",
    regionin = "county:077+state:42"
  )
hrecht commented 4 years ago

Hi Sara, the geography name has to be written out as county subdivision, not cousub. You can see the geography names with

response_geos <- listCensusMetadata(
    name = "dec/responserate",
    vintage = 2020,
    type = "geographies"
)

This works

lv_responses <- getCensus(
    name = "dec/responserate",
    vintage = 2020,
    vars = c("NAME", "RESP_DATE", "CRRALL", "CRRINT"),
    region = "county subdivision:*",
    regionin = "state:42"
)

Hope that helps.