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

county subdivision not supported? #121

Closed
soungl opened this issue Oct 4, 2018 · 8 comments

Comments

Projects
None yet
3 participants
@soungl
Copy link

commented Oct 4, 2018

Is "County Subdivision" not supported any more? The code below returns the following error message. Thank you.

Error: Your API call has errors. The API message returned is error: unknown/unsupported geography heirarchy.

get_acs(geography = "county subdivision",
variables = c(medianIncome = "B19013_001"),
year = 2016,
geometry = FALSE)

@walkerke

This comment has been minimized.

Copy link
Owner

commented Oct 4, 2018

You'll need to specify a state:

get_acs(geography = "county subdivision",
        variables = c(medianIncome = "B19013_001"),
        state = "TX")

Getting data from the 2012-2016 5-year ACS
Using FIPS code '48' for state 'TX'
# A tibble: 862 x 5
   GEOID      NAME                                          variable    estimate   moe
   <chr>      <chr>                                         <chr>          <dbl> <dbl>
 1 4800190650 Cayuga CCD, Anderson County, Texas            medianInco53750 21623
 2 4800191205 Elkhart CCD, Anderson County, Texas           medianInco42194  4979
 3 4800191390 Frankston CCD, Anderson County, Texas         medianInco52099  4865
 4 4800192615 Montalba-Tennessee Colony CCD, Anderson CounmedianInco51875  9801
 5 4800192945 Palestine CCD, Anderson County, Texas         medianInco38556  3938
 6 4800390085 Andrews North CCD, Andrews County, Texas      medianInco71154  7246
 7 4800390090 Andrews South CCD, Andrews County, Texas      medianInco57419 24519
 8 4800591045 Diboll CCD, Angelina County, Texas            medianInco45304  4623
 9 4800591855 Huntington CCD, Angelina County, Texas        medianInco48769  7545
10 4800592325 Lufkin CCD, Angelina County, Texas            medianInco41895  1986
# ... with 852 more rows

@walkerke walkerke closed this Oct 4, 2018

@soungl

This comment has been minimized.

Copy link
Author

commented Oct 4, 2018

@mfherman

This comment has been minimized.

Copy link
Contributor

commented Oct 5, 2018

I like to make use of the map family of functions from the purrr package for a task like this. Using the formula syntax with map_dfr() makes it very concise.

library(tidyverse)
library(tidycensus)

my_states <- c("NH", "ME", "VT", "NY")

map_dfr(my_states, ~ get_acs(geography = "county subdivision",
                             variables = c(medianIncome = "B19013_001"),
                             state = .)
        )
#> Getting data from the 2012-2016 5-year ACS
#> Getting data from the 2012-2016 5-year ACS
#> Getting data from the 2012-2016 5-year ACS
#> Getting data from the 2012-2016 5-year ACS
#> # A tibble: 2,071 x 5
#>    GEOID     NAME                                variable   estimate   moe
#>    <chr>     <chr>                               <chr>         <dbl> <dbl>
#>  1 33001010… Alton town, Belknap County, New Ha… medianInc…    76676 11670
#>  2 33001032… Barnstead town, Belknap County, Ne… medianInc…    70037  6457
#>  3 33001047… Belmont town, Belknap County, New … medianInc…    60938  7012
#>  4 33001106… Center Harbor town, Belknap County… medianInc…    70625 20060
#>  5 33001287… Gilford town, Belknap County, New … medianInc…    63125 10372
#>  6 33001289… Gilmanton town, Belknap County, Ne… medianInc…    68085  6917
#>  7 33001401… Laconia city, Belknap County, New … medianInc…    48893  4719
#>  8 33001471… Meredith town, Belknap County, New… medianInc…    63846  8902
#>  9 33001515… New Hampton town, Belknap County, … medianInc…    74293  8257
#> 10 33001673… Sanbornton town, Belknap County, N… medianInc…    72721  7287
#> # … with 2,061 more rows

Created on 2018-10-04 by the reprex package (v0.2.1)

@soungl

This comment has been minimized.

Copy link
Author

commented Oct 5, 2018

@mfherman

This comment has been minimized.

Copy link
Contributor

commented Oct 5, 2018

It appears that the Census API doesn't allow you to call all county subdivision in a given state for the Decennial Census files, while you can for the ACS subject/detail tables. So one approach I came up with is to use the fips_codes dataset which is included in tidycensus to select all the counties within your requested states. Then you an loop over each of the state/county FIPS code combinations and request the county subdivisions in each of the counties. It hits the Census API for each county (102 in this case) but I'm not sure of a different solution at the moment.

library(tidyverse)
library(tidycensus)

my_states <- c("NH", "ME", "VT", "NY")

my_counties <- fips_codes %>%
  filter(state %in% my_states)

vars10 <- c(seasonalHome = "H005006", # vacant for seasonal, recreational, or occasional use
            totalHome = "H001001", # total number of homoes in 2010
            pop2010 = "P001001") # total population in 2010

map2_dfr(
  my_counties$state_code, my_counties$county_code,
  ~ get_decennial(
    geography = "county subdivision",
    state = .x,
    county = .y,
    variables = vars10,
    year = 2010,
    geometry = FALSE
    )
  )
#> # A tibble: 6,213 x 4
#>    GEOID      NAME                                       variable    value
#>    <chr>      <chr>                                      <chr>       <dbl>
#>  1 2300138565 Leeds town, Androscoggin County, Maine     seasonalHo…    91
#>  2 2300102060 Auburn city, Androscoggin County, Maine    seasonalHo…   170
#>  3 2300119105 Durham town, Androscoggin County, Maine    seasonalHo…     9
#>  4 2300129255 Greene town, Androscoggin County, Maine    seasonalHo…   142
#>  5 2300138740 Lewiston city, Androscoggin County, Maine  seasonalHo…    96
#>  6 2300140035 Lisbon town, Androscoggin County, Maine    seasonalHo…    20
#>  7 2300140665 Livermore town, Androscoggin County, Maine seasonalHo…   168
#>  8 2300140770 Livermore Falls town, Androscoggin County… seasonalHo…    24
#>  9 2300144585 Mechanic Falls town, Androscoggin County,… seasonalHo…     9
#> 10 2300146160 Minot town, Androscoggin County, Maine     seasonalHo…     6
#> # … with 6,203 more rows

Created on 2018-10-05 by the reprex package (v0.2.1)

@soungl

This comment has been minimized.

Copy link
Author

commented Oct 6, 2018

@mfherman

This comment has been minimized.

Copy link
Contributor

commented Oct 7, 2018

FYI -- I just wrote up some of this in a little more detail on my blog:

https://mattherman.info/blog/tidycensus-mult/

@soungl

This comment has been minimized.

Copy link
Author

commented Oct 8, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.