Skip to content

Instantly share code, notes, and snippets.

View smach's full-sized avatar

Sharon Machlis smach

View GitHub Profile
@smach
smach / dt_combine_polygons.R
Created February 21, 2021 14:53
RStudio code snippet for combining smaller polygons (precincts) into larger polygons (City Council Districts)
snippet mygis_dt_merge_districts_2_precincts
${1:my_geography} <- as.data.table(${1:my_geography})
${2:my_new_geography} <- fram2[, .(geometry = st_union(geometry)), by = ${3:larger_district}]
${2:my_new_geography} <- sf::st_sf(${2:my_new_geography})
# test plot
# ggplot(${2:my_new_geography}, aes(geometry=geometry)) +
# geom_sf()
State TotalDistributed TotalAdministered ReportDate Used PctUsed color
CT 740300 542414 2021-02-08 0.732694853437796 73.3 #3366CC
MA 1247600 806376 2021-02-08 0.646341776210324 64.6 #003399
ME 254550 178449 2021-02-08 0.701037124337065 70.1 #3366CC
NH 257700 166603 2021-02-08 0.646499805975941 64.6 #3366CC
NY 3378300 2418074 2021-02-08 0.715766509783027 71.6 #3366CC
RI 192300 120484 2021-02-08 0.626541861674467 62.7 #3366CC
VT 116075 90328 2021-02-08 0.778186517337928 77.8 #3366CC
CT 740300 533941 2021-02-07 0.721249493448602 72.1 #3366CC
MA 1247600 780268 2021-02-07 0.625415197178583 62.5 #003399
@smach
smach / election_night_live_model.R
Created October 31, 2020 18:25 — forked from elliottmorris/election_night_live_model.R
A live election-night prediction model using The Economist's pre-election forecast
#' Description
#' This file runs a live election-night forecast based on The Economist's pre-election forecasting model
#' available at projects.economist.com/us-2020-forecast/president.
#' It is resampling model based on https://pkremp.github.io/update_prob.html.
#' This script does not input any real election results! You will have to enter your picks/constraints manually (scroll to the bottom of the script).
#'
#' Licence
#' This software is published by *[The Economist](https://www.economist.com)* under the [MIT licence](https://opensource.org/licenses/MIT). The data generated by *The Economist* are available under the [Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/).
#' The licences include only the data and the software authored by *The Economist*, and do not cover any *Economist* content or third-party data or content made available using the software. More information about licensing, syndication and the copyright of *Economist* content can be fou
library(reactable)
library(dplyr)
red_pal <- function(x) rgb(colorRamp(c("#FFCDD2FF", "#C62828FF"))(x), maxColorValue = 255)
blue_pal <- function(x) rgb(colorRamp(c("#BBDEFBFF", "#1565C0FF"))(x), maxColorValue = 255)
mtcars %>%
select(cyl, mpg) %>%
reactable(
pagination = FALSE,
@smach
smach / geocode_courthouses.R
Last active December 24, 2019 15:58
Code to geocode courthouse list
# 1. Scrape tables. You can do that with the Table Capture Chrome extension, or you can do it with R.
# Download the entire html document so I don't need to keep hammering the Wikipedia server
library(htmltab)
library(rvest)
library(purrr)
library(dplyr)
@smach
smach / TidyTuesdayAltChallenge.R
Last active December 11, 2019 00:50
Reshaping some very messy data
library(dplyr)
library(tidyr)
library(janitor)
library(stringr)
starwars_garbage_data1 <- data.frame(
stringsAsFactors = FALSE,
v1 = c(
"Character Name", "C-3PO", "Person-film ID", "2218529825", "7731900678",
"123598423", "238952395", "6232048034", "3036308047",
@smach
smach / ImportZipCodes.R
Created June 20, 2019 17:15
See how readr imports Zip Codes compared with base R
price_median1 <- readr::read_csv("https://raw.githubusercontent.com/smach/BU-dataviz-workshop-2019/master/data/zillow_data_median_sf_price.csv")
str(price_median1)
price_median2 <- read.csv("https://raw.githubusercontent.com/smach/BU-dataviz-workshop-2019/master/data/zillow_data_median_sf_price.csv")
str(price_median2)
@smach
smach / app.R
Last active April 26, 2019 13:07
if(!require(pacman)){
install.packages("pacman")
}
p_load(ggplot2, dplyr, janitor)
district <-c("A","B","C","A","B", "C")
money <-c(500,324,245,654,234, 232)
year <- c("2001", "2001", "2001", "2002", "2002", "2002")
df <- data.frame(district, money, year, stringsAsFactors = FALSE)
total_by_year <- df %>%
group_by(year) %>%
# Survey data file downloaded manually from the CDC https://www.cdc.gov/brfss/annual_data/annual_2017.html
surveydatafile <- "LLCP2017.ASC"
# I used datapasta package's df_paste() function to create the data frame below,
# after copying the metadata table into my clipboard. Metadata is here:
# https://www.cdc.gov/brfss/annual_data/2017/llcp_varlayout_17_onecolumn.html
column_meta_data <- data.frame(stringsAsFactors=FALSE,
Starting.Column = c(1L, 17L, 19L, 19L, 21L, 23L, 32L, 36L, 36L, 63L, 64L,
65L, 66L, 67L, 68L, 69L, 71L, 73L, 75L, 76L, 77L, 78L,
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.