Skip to content

Instantly share code, notes, and snippets.

@benjamin-chan
benjamin-chan / getCMS.R
Last active September 5, 2024 00:16
R function to query CMS data via API
getCMS <- function(distributionId,
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(where)) {
@benjamin-chan
benjamin-chan / HPA_palette.R
Last active August 19, 2024 22:17
HPA color palette for R
pal <- c("#064276", # OHA Blue
"#EC5A24", # OHA Orange
"#009F98", # Sea Glass
"#752E71", # Beauty Berry
"#FCB53B", # Oregon Sunshine
"#D6DBE9", # OHA Blue (background tint)
"#E6F0EF", # Sea Glass (background tint)
"#E9E1E8", # Beauty Berry (background tint)
"#FFF1DC", # Oregon Sunshine (background tint)
"#BC4010", # OHA Orange (dark)
@benjamin-chan
benjamin-chan / getACSbyZCTA.r
Last active June 24, 2024 21:40
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 = 2022) {
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 = .) %>%
@benjamin-chan
benjamin-chan / getCensusData.R
Last active September 23, 2020 19:26
Example R snippet to wrangle US Census data via API
library(magrittr)
library(readr)
library(tidyr)
library(censusapi)
name <- "acs/acs5" # See https://api.census.gov/data.html for list of tables for the name argument for listCensusMetadata()
vintage <- 2012
metadata <-
listCensusMetadata(name = name, vintage = vintage) %>%
@benjamin-chan
benjamin-chan / birthday_problem.R
Created January 27, 2020 23:00
Simulate the Birthday Problem
# Ref: https://en.wikipedia.org/wiki/Birthday_problem
# Ref: https://www.scientificamerican.com/article/bring-science-home-probability-birthday-paradox/
library(magrittr)
library(dplyr)
people <- 23
simulations <- 1e5
expand.grid(id = 1:people,
sim = 1:simulations) %>%
mutate(bday = sample(365, simulations * people, replace = TRUE)) %>%
group_by(sim) %>%
@benjamin-chan
benjamin-chan / commonIDs.R
Last active January 27, 2020 15:43
Identify common IDs across paired groups
library(magrittr)
library(dplyr)
buildSample <- function(hosp, pop = 500, samp = 100) {
data.frame(hosp = hosp,
id = sample(pop, samp, replace = TRUE),
stringsAsFactors = FALSE) %>%
mutate(z = rnorm(nrow(.)))
}
df1 <- buildSample("A")
df2 <- buildSample("B")