Skip to content

Instantly share code, notes, and snippets.

@ehbick01
Created November 20, 2017 20:29
Show Gist options
  • Save ehbick01/1746d6ef2e9d5f74d0a80b83b75b2a45 to your computer and use it in GitHub Desktop.
Save ehbick01/1746d6ef2e9d5f74d0a80b83b75b2a45 to your computer and use it in GitHub Desktop.
Using `tidycensus` to pull poverty rates by census tract and limit to high-poverty tracts (>= 35% poverty)
## Load Packages
library(tidycensus)
library(purrr)
## Grab Data
# Set API key
census_api_key("your_api_key_here")
# Identify state FIPS codes
us <- unique(fips_codes$state)[1:51]
# Grab tract-level population
totalpop <- map_df(us, function(x) {
get_acs(geography = "tract",
variables = "B01003_001",
state = x)
})
# Grab tract-level poverty populations
totalpov <- map_df(us, function(x) {
get_acs(geography = "tract",
variables = "B17001_002",
state = x)
})
## Calculate Rate
# Merge totalpop with totalpov and calculate percent_poverty
total_data <- totalpop %>%
rename(total_population = estimate) %>%
select(GEOID,
NAME,
total_population) %>%
left_join(totalpov %>%
rename(total_poverty = estimate) %>%
select(GEOID,
NAME,
total_poverty),
by = c("GEOID",
"NAME")) %>%
mutate(percent_poverty = total_poverty / total_population) %>%
filter(percent_poverty >= 0.35)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment