Skip to content

Instantly share code, notes, and snippets.

@ctesta01
Last active November 13, 2020 15:35
Show Gist options
  • Select an option

  • Save ctesta01/f7428d0ddd0545caf2fc2b9c8eb2cdac to your computer and use it in GitHub Desktop.

Select an option

Save ctesta01/f7428d0ddd0545caf2fc2b9c8eb2cdac to your computer and use it in GitHub Desktop.
Save Area Based Measures
library(tidyverse)
library(tidycensus)
library(magrittr)
variables_dict <-
tibble::tribble(
~var, ~desc,
"B03002_001", 'total_race', "total pop for race/non-hispanic estimate",
"B03002_003", 'white_nh', "white non-hispanic pop",
"B25014_001", 'total_crowd', "total for occupants by room by owner/renter",
"B25014_005", 'crowd1_own', "owner occupied occupants by room - 1.01-1.5",
"B25014_006", 'crowd2_own', "owner occupied occupants by room - 1.51-2",
"B25014_007", 'crowd3_own', "owner occupied occupants by room - 2.01+",
"B25014_011", 'crowd1_rent', "renter occupied occupants by room - 1.01-1.5",
"B25014_012", 'crowd2_rent', "renter occupied occupants by room - 1.51-2",
"B25014_013", 'crowd3_rent', "renter occupied occupants by room - 2.01+",
"B05010_001", 'total_pov', "total for poverty estimate",
"B05010_002", 'under_pov', "under poverty line",
"B19001_001", 'hhinc_total', "total pop for household income estimates",
"B19001A_014", 'hhinc_w_1', "white n.h. pop with household income $100 000 to $124 999",
"B19001A_015", 'hhinc_w_2', "white n.h. pop with household income $125k-149 999k",
"B19001A_016", 'hhinc_w_3', "white n.h. pop with household income $150k-199 999k",
"B19001A_017", 'hhinc_w_4', "white n.h. pop with household income $200k+",
"B19001B_002", 'hhinc_b_1', "black pop with household income <$10k",
"B19001B_003", 'hhinc_b_2', "black pop with household income $10k-14 999k",
"B19001B_004", 'hhinc_b_3', "black pop with household income $15k-19 999k",
"B19001B_005", 'hhinc_b_4', "black pop with household income $20k-24 999k",
"B11017_001", 'total_multigen',"total homes for multigenerational estimate",
"B11017_002", 'multigen_hhs', "multigenerational households"
)
# create a named vector, rename_vars, which has elements that are the
# acs variables we request and convenient, human readable names.
#
# used below twice: to request the data, and to rename the data
rename_vars <- setNames(variables_dict$orig_var, variables_dict$var)
# request acs data
us_county_data <- get_acs(geography = "county",
variables = variables_dict$orig_var)
# pivot to a wide format for renaming, dropping the margin of error data
us_county_data_pivotted <- us_county_data %>% as.data.frame() %>% select(-moe) %>%
pivot_wider(names_from = variable, values_from = estimate)
# rename the columns using our rename_vars
us_county_data_pivotted %<>% rename(!!rename_vars)
# construct our area-based-socioeconomic/demographic-measures
us_county_data_pivotted %<>% mutate(
# pct people of color
pct_poc = (total_race - white_nh) / total_race * 100,
# pct crowding
pct_crowd = (crowd1_own + crowd2_own + crowd3_own + crowd1_rent + crowd2_rent + crowd3_rent) / total_crowd * 100,
# pct poverty
pct_pov = under_pov / total_pov * 100,
# racialized economic segregation
wnhb_inc_ice = ((hhinc_w_1 + hhinc_w_2 + hhinc_w_3 + hhinc_w_4) -
(hhinc_b_1 + hhinc_b_2 + hhinc_b_3 + hhinc_b_4)) / hhinc_total,
# percentage of households which are multigenerational
pct_multigen = multigen_hhs / total_multigen * 100
)
# save data
saveRDS(
us_county_data_pivotted,
'us_county_absms.rds'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment