Skip to content

Instantly share code, notes, and snippets.

View mfherman's full-sized avatar

Matt Herman mfherman

View GitHub Profile
@mfherman
mfherman / census-api-discovery.R
Last active October 26, 2022 21:10
automatically find year of most recent acs data available via api and use as input parameter to get_acs()
library(tidyverse)
library(tidycensus)
library(jsonlite)
# get all available census api endpoints
census_api_discovery <- fromJSON("https://api.census.gov/data.json")
# parse responses into df with vintage and dataset name
api_name_vintage <- tibble(
vintage = census_api_discovery$dataset$c_vintage,
@mfherman
mfherman / omaha_violent_crime_trends.R
Created September 9, 2022 15:18
Plot violent crime trends from Omaha PD 2016-2021 and compare to 2022
@mfherman
mfherman / overlapping-terms.R
Last active January 7, 2022 18:33
ggplot2 code to create chart showing overlapping probation terms with offenses
library(tidyverse)
df <- tribble(
~agency, ~offense, ~type, ~date,
"Agency A", "Drug", "sentence_dt", as.Date("2016-01-04"),
"Agency A", "Person", "sentence_dt", as.Date("2017-07-01"),
"Agency A", "Drug", "start_dt", as.Date("2016-04-04"),
"Agency A", "Drug", "end_dt", as.Date("2018-12-09"),
"Agency B", "Person", "sentence_dt", as.Date("2017-01-30"),
"Agency B", "Person", "start_dt", as.Date("2017-01-30"),
library(tidyverse)
df <- tribble(
~agency, ~offense, ~type, ~date,
"Agency A", "Drug", "sentence_dt", as.Date("2016-01-04"),
"Agency A", "Person", "sentence_dt", as.Date("2017-07-01"),
"Agency A", "Drug", "start_dt", as.Date("2016-04-04"),
"Agency A", "Drug", "end_dt", as.Date("2018-12-09"),
"Agency B", "Person", "sentence_dt", as.Date("2017-01-30"),
"Agency B", "Person", "start_dt", as.Date("2017-01-30"),
@mfherman
mfherman / census-geo-add.R
Created July 4, 2021 14:07
Which census geographies add up?
library(tidycensus)
library(tidyverse)
state <- get_decennial(
geography = "state",
variables = "P001001",
state = "CA"
)
tract <- get_decennial(
@mfherman
mfherman / healthcare_race_pums.R
Last active August 7, 2020 20:01
Estimate race and ethnicity of health care workers by PUMA using PUMS data
## Estimate race and ethnicity of health care workers by PUMA
# NOTE: this requires the github version of tidycensus
# remotes::install_github("walkerke/tidycensus")
library(tidycensus)
library(tidyverse)
# get industry and race vars from census api
ind_pums <- get_pums(
variables = c("PUMA", "INDP", "RAC1P", "HISP"),
@mfherman
mfherman / sf_pt_poly.r
Created May 9, 2018 18:44
sf point in polygon
library(tidyverse)
library(tidycensus)
library(sf)
library(tmap)
options(tigris_use_cache = TRUE)
sro <- read_csv("https://github.com/mfherman/r-study-group/raw/master/nyc_sro.csv")
sro_sf <- sro %>%