walkerke / tidycensus

Load US Census boundary and attribute data as 'tidyverse' and 'sf'-ready data frames in R
https://walker-data.com/tidycensus
Other
639 stars 100 forks source link

get_decennial returning error on tract geography #129

Closed arthurgailes closed 5 years ago

arthurgailes commented 5 years ago

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 commented 5 years ago

This is because the Census API requires a state/county combination to retrieve tracts from the Decennial Census. This issue is similar to https://github.com/walkerke/tidycensus/issues/121#issuecomment-427526779 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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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. https://github.com/walkerke/tidycensus/issues/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 commented 5 years ago

Thanks @mfherman !