Created
August 16, 2022 15:44
-
-
Save walkerke/c52fae29df307b7c852e045590bf104d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
library(tidycensus) | |
library(tidyverse) | |
library(tigris) | |
library(sf) | |
options(tigris_use_cache = TRUE) | |
# You'll need a Census API key to get this to work | |
# census_api_key("YOUR KEY HERE") | |
vt_tract_pop <- get_decennial( | |
geography = "tract", | |
variables = "P1_001N", | |
state = "VT", | |
year = 2020, | |
geometry = TRUE | |
) | |
vt_county_pop <- vt_tract_pop %>% | |
mutate(county_fips = str_sub(GEOID, 1, 5)) %>% | |
group_by(county_fips) %>% | |
summarize(pop = sum(value, na.rm = TRUE)) | |
leafsync::sync( | |
mapview::mapview(vt_tract_pop, zcol = "value", layer.name = "Tract population"), | |
mapview::mapview(vt_county_pop, zcol = "pop", layer.name = "County population") | |
) | |
# Example 1: regular group by / summarize. Crashes my computer! | |
# Will probably work with a projected CRS or sf::sf_use_s2(FALSE) but will be slow | |
tx_income_groups <- get_acs( | |
geography = "tract", | |
table = "B19001", | |
state = "TX", | |
year = 2020, | |
geometry = TRUE | |
) %>% | |
filter(variable != "B19001_001") %>% | |
mutate(bracket = case_when( | |
variable > "B19001_013" ~ "Above $100k", | |
TRUE ~ "Below $100k" | |
)) %>% | |
group_by(GEOID, bracket) %>% | |
summarize(n_households = sum(estimate, na.rm = TRUE)) | |
# Example 2: do_union = FALSE | |
tictoc::tic() | |
tx_income_groups <- get_acs( | |
geography = "tract", | |
table = "B19001", | |
state = "TX", | |
year = 2020, | |
geometry = TRUE | |
) %>% | |
filter(variable != "B19001_001") %>% | |
mutate(bracket = case_when( | |
variable > "B19001_013" ~ "Above $100k", | |
TRUE ~ "Below $100k" | |
)) %>% | |
group_by(GEOID, bracket) %>% | |
summarize(n_households = sum(estimate, na.rm = TRUE), | |
do_union = FALSE) | |
tictoc::toc() | |
# Example 3: get tracts independently | |
tictoc::tic() | |
tx_tracts <- tracts("TX", cb = TRUE, year = 2020) %>% | |
select(GEOID) | |
tx_income_groups <- get_acs( | |
geography = "tract", | |
table = "B19001", | |
state = "TX", | |
year = 2020, | |
geometry = FALSE | |
) %>% | |
filter(variable != "B19001_001") %>% | |
mutate(bracket = case_when( | |
variable > "B19001_013" ~ "Above $100k", | |
TRUE ~ "Below $100k" | |
)) %>% | |
group_by(GEOID, bracket) %>% | |
summarize(n_households = sum(estimate, na.rm = TRUE)) | |
tx_income_groups_sf <- tx_tracts %>% | |
left_join(tx_income_groups, by = "GEOID") | |
tictoc::toc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment