Skip to content

Instantly share code, notes, and snippets.

@benjamin-chan
Last active September 29, 2025 14:58
Show Gist options
  • Save benjamin-chan/4c31e0767486741982b3fdc5d5b7b57c to your computer and use it in GitHub Desktop.
Save benjamin-chan/4c31e0767486741982b3fdc5d5b7b57c to your computer and use it in GitHub Desktop.
Get % Medicaid by Oregon congressional district from 5-Year ACS

Medicaid enrollment for Oregon congressional districts

Sys.getenv("CENSUS_API_KEY")  # load pre-installed Census API key

library(magrittr)
library(dplyr)
library(tidyr)
library(tidycensus)

variables <- "S2704_C02_006"
labels <-
  load_variables(2023, dataset = "acs5/subject", cache = TRUE) %>%
  mutate(label = gsub(":$", "", label)) %>%
  filter(name %in% variables) %>%
  separate_wider_delim(label, names_sep = "", delim = "!!", too_few = "align_start") %>%
  rename(variable = name)
get_acs(geography = "congressional district", state = "OR", variables = variables, summary_var = "S2704_C01_001") %>%
  mutate(proportion = estimate / summary_est) %>%
  arrange(-proportion) %>%
  select(-ends_with("moe")) %>%
  inner_join(labels) %>%
  knitr::kable()
GEOID NAME variable estimate summary_est proportion label1 label2 label3 label4 concept
4102 Congressional District 2 (118th Congress), Oregon S2704_C02_006 202410 694105 0.2916129 Estimate Public Coverage COVERAGE ALONE OR IN COMBINATION Medicaid/means-tested public coverage alone or in combination Public Health Insurance Coverage by Type and Selected Characteristics
4104 Congressional District 4 (118th Congress), Oregon S2704_C02_006 179301 703108 0.2550120 Estimate Public Coverage COVERAGE ALONE OR IN COMBINATION Medicaid/means-tested public coverage alone or in combination Public Health Insurance Coverage by Type and Selected Characteristics
4103 Congressional District 3 (118th Congress), Oregon S2704_C02_006 159027 693656 0.2292592 Estimate Public Coverage COVERAGE ALONE OR IN COMBINATION Medicaid/means-tested public coverage alone or in combination Public Health Insurance Coverage by Type and Selected Characteristics
4106 Congressional District 6 (118th Congress), Oregon S2704_C02_006 158463 697577 0.2271620 Estimate Public Coverage COVERAGE ALONE OR IN COMBINATION Medicaid/means-tested public coverage alone or in combination Public Health Insurance Coverage by Type and Selected Characteristics
4105 Congressional District 5 (118th Congress), Oregon S2704_C02_006 140125 707151 0.1981543 Estimate Public Coverage COVERAGE ALONE OR IN COMBINATION Medicaid/means-tested public coverage alone or in combination Public Health Insurance Coverage by Type and Selected Characteristics
4101 Congressional District 1 (118th Congress), Oregon S2704_C02_006 120846 701349 0.1723051 Estimate Public Coverage COVERAGE ALONE OR IN COMBINATION Medicaid/means-tested public coverage alone or in combination Public Health Insurance Coverage by Type and Selected Characteristics
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment