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

Help with "american indian area/alaska native area/hawaiian home land" geography #323

Closed koehnl closed 3 years ago

koehnl commented 3 years ago

Hi - If I just want census tracts or locations for American Indian areas/land why won't something like this work? areas <- get_decennial(geography = "american indian area/alaska native area/hawaiian home land", year = 2010, variables = c(medincome = "B19013_001"), state = "WA", geometry = TRUE)

I think the geography name is not found but it's in the table of geographies - error returned:

Getting data from the 2010 decennial Census Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set options(tigris_use_cache = TRUE). Using FIPS code '53' for state 'WA' Error : Your API call has errors. The API message returned is Error report

HTTP Status 404 - /data/2010/dec/sf3

. Error in gather.default(., key = variable, value = value, -GEOID, -NAME) : object 'NAME' not found

Thank you!

walkerke commented 3 years ago

You are trying to use an American Community Survey variable (B19013_001) to get data from the decennial Census, which won't work as they are different datasets. As an FYI: the decennial Census does not include information on most demographic characteristics (including income) as of the 2010 Census; that information is found in the ACS, which tidycensus grabs with get_acs(). You can get the data you need from the 5-year ACS like this (shown for the most recent sample, 2014-2018):

library(tidycensus)

areas <- get_acs(
  geography = "american indian area/alaska native area/hawaiian home land",
  year = 2018,
  variables = c(medincome = "B19013_001"),
  geometry = TRUE
)

Note that I omitted the state argument as Native American areas are not organized within states by the Census API. That said, you can extract the requested information from the NAME column:

library(tidyverse)
library(mapview)

library(tidyverse)

wa_areas <- areas %>%
  filter(str_detect(NAME, ", WA"))

mapview(wa_areas)

image

Census tracts for these areas could be obtained using {tigris} and spatial filtering with sf::st_filter(). tidycensus doesn't yet have the capability to get data for tribal tracts/block groups, though I'd like to implement this in a future release.

koehnl commented 3 years ago

Thank you!

koehnl commented 3 years ago

Hi - sorry, one more thing, if you have any ideas for how I could then link these areas to census tracts (pulled from get_acs) that would be very helpful. maybe an intersection between the geometry output. thanks!

mfherman commented 3 years ago

Just saw your question, @koehnl! The last step would be to get tract geometries (either via get_acs() or tigris::tracts() and do a spatial join using the sf package. By default, st_filter() runs an intersection of your two geometries, though you can use other spatial predicate functions (e.g. contains, within if you like.

library(tidycensus)
library(tidyverse)
library(mapview)
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1

areas <- get_acs(
  geography = "american indian area/alaska native area/hawaiian home land",
  year = 2018,
  variables = c(medincome = "B19013_001"),
  geometry = TRUE
  )
#> Getting data from the 2014-2018 5-year ACS
#> Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
#>   |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   1%  |                                                                              |=                                                                     |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |======                                                                |   8%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  12%  |                                                                              |==========                                                            |  14%  |                                                                              |===========                                                           |  15%  |                                                                              |============                                                          |  17%  |                                                                              |=============                                                         |  18%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  21%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  23%  |                                                                              |=================                                                     |  24%  |                                                                              |==================                                                    |  25%  |                                                                              |===================                                                   |  27%  |                                                                              |====================                                                  |  28%  |                                                                              |====================                                                  |  29%  |                                                                              |=====================                                                 |  30%  |                                                                              |=======================                                               |  32%  |                                                                              |========================                                              |  34%  |                                                                              |=========================                                             |  35%  |                                                                              |=========================                                             |  36%  |                                                                              |==========================                                            |  37%  |                                                                              |===========================                                           |  39%  |                                                                              |============================                                          |  41%  |                                                                              |=============================                                         |  42%  |                                                                              |==============================                                        |  42%  |                                                                              |===============================                                       |  44%  |                                                                              |================================                                      |  46%  |                                                                              |=================================                                     |  47%  |                                                                              |==================================                                    |  49%  |                                                                              |====================================                                  |  51%  |                                                                              |=====================================                                 |  52%  |                                                                              |======================================                                |  54%  |                                                                              |=======================================                               |  56%  |                                                                              |========================================                              |  58%  |                                                                              |=========================================                             |  59%  |                                                                              |===========================================                           |  61%  |                                                                              |===========================================                           |  62%  |                                                                              |============================================                          |  63%  |                                                                              |=============================================                         |  64%  |                                                                              |==============================================                        |  65%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  68%  |                                                                              |=================================================                     |  69%  |                                                                              |=================================================                     |  70%  |                                                                              |==================================================                    |  71%  |                                                                              |===================================================                   |  73%  |                                                                              |====================================================                  |  74%  |                                                                              |=====================================================                 |  76%  |                                                                              |======================================================                |  78%  |                                                                              |=======================================================               |  79%  |                                                                              |========================================================              |  79%  |                                                                              |=========================================================             |  81%  |                                                                              |==========================================================            |  83%  |                                                                              |===========================================================           |  85%  |                                                                              |============================================================          |  86%  |                                                                              |==============================================================        |  88%  |                                                                              |===============================================================       |  90%  |                                                                              |================================================================      |  91%  |                                                                              |=================================================================     |  92%  |                                                                              |=================================================================     |  93%  |                                                                              |==================================================================    |  95%  |                                                                              |===================================================================   |  96%  |                                                                              |====================================================================  |  97%  |                                                                              |===================================================================== |  98%  |                                                                              |======================================================================| 100%

wa_tracts <- tigris::tracts("wa", cb = TRUE)
#>   |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   2%  |                                                                              |===                                                                   |   4%  |                                                                              |====                                                                  |   5%  |                                                                              |=====                                                                 |   6%  |                                                                              |======                                                                |   8%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  12%  |                                                                              |=========                                                             |  12%  |                                                                              |==========                                                            |  14%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  18%  |                                                                              |=============                                                         |  18%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  22%  |                                                                              |=================                                                     |  24%  |                                                                              |==================                                                    |  26%  |                                                                              |===================                                                   |  27%  |                                                                              |====================                                                  |  28%  |                                                                              |=====================                                                 |  29%  |                                                                              |=====================                                                 |  30%  |                                                                              |======================                                                |  31%  |                                                                              |======================                                                |  32%  |                                                                              |========================                                              |  34%  |                                                                              |=========================                                             |  36%  |                                                                              |==========================                                            |  37%  |                                                                              |===========================                                           |  38%  |                                                                              |============================                                          |  40%  |                                                                              |=============================                                         |  41%  |                                                                              |=============================                                         |  42%  |                                                                              |==============================                                        |  43%  |                                                                              |===============================                                       |  44%  |                                                                              |================================                                      |  46%  |                                                                              |==================================                                    |  48%  |                                                                              |===================================                                   |  50%  |                                                                              |====================================                                  |  52%  |                                                                              |======================================                                |  54%  |                                                                              |=======================================                               |  55%  |                                                                              |=======================================                               |  56%  |                                                                              |========================================                              |  57%  |                                                                              |========================================                              |  58%  |                                                                              |=========================================                             |  59%  |                                                                              |==========================================                            |  60%  |                                                                              |===========================================                           |  61%  |                                                                              |===========================================                           |  62%  |                                                                              |=============================================                         |  64%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  67%  |                                                                              |===============================================                       |  68%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  70%  |                                                                              |==================================================                    |  71%  |                                                                              |==================================================                    |  72%  |                                                                              |====================================================                  |  74%  |                                                                              |====================================================                  |  75%  |                                                                              |=====================================================                 |  76%  |                                                                              |======================================================                |  77%  |                                                                              |======================================================                |  78%  |                                                                              |========================================================              |  80%  |                                                                              |=========================================================             |  81%  |                                                                              |==========================================================            |  83%  |                                                                              |===========================================================           |  85%  |                                                                              |============================================================          |  85%  |                                                                              |=============================================================         |  87%  |                                                                              |==============================================================        |  89%  |                                                                              |===============================================================       |  89%  |                                                                              |===============================================================       |  91%  |                                                                              |================================================================      |  91%  |                                                                              |=================================================================     |  93%  |                                                                              |===================================================================   |  95%  |                                                                              |====================================================================  |  97%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================| 100%

wa_areas <- areas %>%
  filter(str_detect(NAME, ", WA"))

area_tract <- wa_tracts %>% 
  st_join(wa_areas)
#> although coordinates are longitude/latitude, st_intersects assumes that they are planar

area_tracts <- st_filter(wa_tracts, wa_areas)
#> although coordinates are longitude/latitude, st_intersects assumes that they are planar

mapview(area_tracts)

Created on 2021-01-25 by the reprex package (v0.3.0)

koehnl commented 2 years ago

I never said thank you! thank you! I think I have colleagues that this will help now too.