Created
November 20, 2017 20:29
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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