Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_decennial returning error on tract geography #129

Closed
arthurgailes opened this issue Nov 6, 2018 · 5 comments
Closed

get_decennial returning error on tract geography #129

arthurgailes opened this issue Nov 6, 2018 · 5 comments

Comments

@arthurgailes
Copy link
Sponsor Contributor

Hello,

Running the code from the example works perfectly, but when I switch "county" to tract, it returns an error

library(tidycensus)
vars10 <- c("P005003", "P005004", "P005006", "P004003")

il <- get_decennial(geography = "tract", variables = vars10, year = 2010,
                    summary_var = "P001001", state = "IL")

Error : One or more of your requested variables is likely not available at the requested geography. Please refine your selection.

Not sure what's causing this, but it's recurring across at least several P tables.

@mfherman
Copy link
Collaborator

mfherman commented Nov 6, 2018

This is because the Census API requires a state/county combination to retrieve tracts from the Decennial Census. This issue is similar to #121 (comment) where a state and county is required to get county subdivision data.

I wrote up some of this in a blog post with plenty of examples. Below is some code that should get what you're looking for.

library(tidyverse)
library(tidycensus)

vars10 <- c("P005003", "P005004", "P005006", "P004003")

il_counties <- fips_codes %>%
 filter(state %in% "IL")

il <- map_dfr(
 il_counties$county_code,
 ~ get_decennial(
   geography = "tract",
   state = "IL",
   county = .x,
   variables = vars10,
   year = 2010,
   summary_var = "P001001",
   geometry = FALSE
   )
 )

head(il)
#> # A tibble: 6 x 5
#>   GEOID      NAME                              variable value summary_value
#>   <chr>      <chr>                             <chr>    <dbl>         <dbl>
#> 1 170010001… Census Tract 1, Adams County, Il… P005003   4370          4627
#> 2 170010002… Census Tract 2.01, Adams County,… P005003   1868          1986
#> 3 170010002… Census Tract 2.02, Adams County,… P005003   2648          2999
#> 4 170010004… Census Tract 4, Adams County, Il… P005003   3493          4322
#> 5 170010005… Census Tract 5, Adams County, Il… P005003   1866          2337
#> 6 170010006… Census Tract 6, Adams County, Il… P005003   3430          3718

Created on 2018-11-06 by the reprex package (v0.2.1)

@walkerke Would it be worth building this logic into tidycensus or do you think users should have to specify state/county in these cases? It is a bit confusing that that the Census API has different requirements for getting Decennial data and ACS data for certain geographies.

@walkerke
Copy link
Owner

walkerke commented Nov 6, 2018

Thanks @mfherman for the detailed response. Yes, I think it is worth building this logic into tidycensus. With the move to the new Census API endpoints, Census removed this functionality; tracts used to be available by state for the decennial Census. This was an issue for the new ACS endpoints as well, but Census fixed it; @loganpowell do you think the API team would be amenable to this request as well?

@mfherman
Copy link
Collaborator

mfherman commented Nov 6, 2018

I'm quite a novice package developer, but I can try to make a start on adding this to tidycensus. Even if they do fix the Census API, it may still be useful for other geographies like county subdivisions and block groups.

@walkerke
Copy link
Owner

walkerke commented Nov 7, 2018

@mfherman I have logic in the package to handle this for block groups, though there are some longstanding improvements I still need to make (e.g. #94). It seems like the hierarchy changes with the move to the new API endpoints are still in flux so I'll try to get some clarity on that.

@arthurgailes
Copy link
Sponsor Contributor Author

Thanks @mfherman !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants