Skip to content

Instantly share code, notes, and snippets.

@benjamin-chan
benjamin-chan / medicaidEnrollment.md
Last active September 29, 2025 14:58
Get % Medicaid by Oregon congressional district from 5-Year ACS
@benjamin-chan
benjamin-chan / scrapePLS.md
Created June 6, 2025 19:24
Scrape Public Libraries Survey (PLS) data
@benjamin-chan
benjamin-chan / zipcodeCentroids.R
Last active April 16, 2025 17:42
HUD population weighted zip code centroids
# [Website](https://hudgis-hud.opendata.arcgis.com/datasets/zip-code-population-weighted-centroids-1/about)
# [API](https://hudgis-hud.opendata.arcgis.com/datasets/zip-code-population-weighted-centroids-1/api)
library(magrittr)
library(dplyr)
library(jsonlite)
# All zip codes, all columns
url <- "https://services.arcgis.com/VTyQ9soqVukalItT/arcgis/rest/services/ZIP_Code_Population_Weighted_Centroids/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json"
centroids <- fromJSON(url)$features
# Oregon zip codes, essential columns only
@benjamin-chan
benjamin-chan / getCMS.R
Last active March 10, 2026 15:44
R function to query CMS data via API
getCMS <- function(datasetID = NULL,
columns = "*",
where = NULL,
limit = 0,
root = "https://data.cms.gov/provider-data/api/1/datastore/sql")
{
require(magrittr)
require(httr)
require(jsonlite)
if (is.null(datasetID)) {
@benjamin-chan
benjamin-chan / OHA_palette.R
Last active May 14, 2026 17:09
Oregon Health Authority color palette for R
pal <- c("#174375", # OHA Blue
"#ec5a24", # OHA Orange
"#00817f", # Sea Glass
"#752e71", # Beauty Berry
"#d5dae8", # Blue BG
"#fff2dd", # Orange BG
"#dceae8", # Sea Glass BG
"#ebdee8", # Beauty Berry BG
"#F2F2F2", # Light gray; requires the use of a border
"#949494", # Medium gray (updated)
@benjamin-chan
benjamin-chan / getACSbyZCTA.r
Last active May 29, 2025 19:04
Grab ACS 5-year data (2017-2021) via API
# US Census ACS 5-year API documentation: https://www.census.gov/data/developers/data-sets/acs-5year.html
# tidycensus documentation: https://walker-data.com/tidycensus/
Sys.getenv("CENSUS_API_KEY") # load pre-installed Census API key
getACSbyZCTA <- function(lookup, table = NULL, variables = NULL, dataset = "acs5/profile", year = 2023) {
require(magrittr)
require(dplyr)
require(tidyr)
require(tidycensus)
@benjamin-chan
benjamin-chan / search.R
Created January 12, 2023 22:30
Search for text in a file
search <- function(file, str) {
text <- readLines(file.path(path, file), warn = FALSE)
df <- data.frame(file = file,
line = 1:length(text),
regex = str,
result = grepl(str, text),
text)
df[df$result == TRUE, ]
}
# Reference: https://www.bls.gov/developers/api_r.htm
library(magrittr)
library(dplyr)
library(devtools)
install_github("mikeasilva/blsAPI") # https://github.com/mikeasilva/blsAPI
library(rjson)
library(blsAPI)
payload <- list("seriesid" = c("CUUR0000SA0L1E"),
"startyear" = 2016,
@benjamin-chan
benjamin-chan / zone9HexCodes.R
Created December 25, 2021 00:45
Hex codes for zone system grayscale
c(rgb( 26, 26, 26, maxColorValue = 255),
rgb( 51, 51, 51, maxColorValue = 255),
rgb( 77, 77, 77, maxColorValue = 255),
rgb(102, 102, 102, maxColorValue = 255),
rgb(127, 127, 127, maxColorValue = 255),
rgb(153, 153, 153, maxColorValue = 255),
rgb(179, 179, 179, maxColorValue = 255),
rgb(204, 204, 204, maxColorValue = 255),
rgb(230, 230, 230, maxColorValue = 255))
@benjamin-chan
benjamin-chan / randomSample.R
Last active January 27, 2022 16:30
Random sample
library(magrittr)
library(dplyr)
c("Alice",
"Bob",
"Carol",
"Dave") %>%
unique() %>%
sample(1e6, replace = TRUE) %>%
data.frame(names = .) %>%