Skip to content

Instantly share code, notes, and snippets.

View rudeboybert's full-sized avatar
💭
Keep it real

Albert Y. Kim rudeboybert

💭
Keep it real
View GitHub Profile
@rudeboybert
rudeboybert / switch.R
Last active October 12, 2016 15:41
Map of states that switched between Trump/Clinton if genders voted separately
# Which states changed colors here:
# http://fivethirtyeight.com/features/election-update-women-are-defeating-donald-trump/
library(ggplot2)
library(maps)
library(mapproj)
library(dplyr)
# Load map of us data
all_states <- map_data("state")
@rudeboybert
rudeboybert / rflip.R
Last active October 31, 2016 15:09
Coin Flipping
# Load packages, including mosaic package
library(dplyr)
library(ggplot2)
library(mosaic)
# Flip a coin once. Try this multiple times:
rflip()
# Flip a coin 10 times. Try this multiple times:
rflip(10)
library(dplyr)
library(ggplot2)
library(rgdal)
library(leaflet)
# Set your working directory to where the folder that contains your shapefiles is at:
VT <- rgdal::readOGR("VT_shapefiles/tl_2015_50_tract.shp", layer = "tl_2015_50_tract")
# The variable in ALAND exists here, i.e. you don't need to join anything
VT@data
# Probability Clinton Wins -- bookie odds 11.07.16
# Code by Jay Emerson for scraping data off web page, and displaying
z <- scan('https://electionbettingodds.com', what="", sep="\n")
z <- z[grep("in last day", z)]
z <- gsub("<[^<>]*>", ",", z)
z <- gsub("^.*',(.*):.*,Clinton ([0-9.]*)%.*$", "\\1,\\2", z)[-1]
writeLines(z, "betfair.csv")
z <- read.csv("betfair.csv", as.is=TRUE, header=FALSE)
names(z) <- c("state", "pclinton")
@rudeboybert
rudeboybert / voter_turnout.R
Last active November 11, 2016 16:03
Voter Turnout for Dems vs Repubs in 2008, 2012, 2016
# Same plot as https://rudeboybert.github.io/MATH116/assets/figure/blue_vs_red.jpg
# But with y-axis starting at 0
library(dplyr)
library(ggplot2)
voter_turnout <- data_frame(
# Make year a categorical variable:
Year = as.factor(c(2008, 2008, 2012, 2012, 2016, 2016)),
Party = c("Democrat", "Republican", "Democrat", "Republican", "Democrat", "Republican"),
Votes = c(69.5, 60, 66, 61, 59, 59)
@rudeboybert
rudeboybert / HW-4_Discussion.R
Last active November 28, 2016 12:59
HW-4 Discussion
library(USAboundaries)
library(rgeos)
library(maptools)
library(RColorBrewer)
# Get polygon data
counties_shp <- us_counties()
counties_polygon <- tidy(counties_shp, region="geoid")
counties_data <- counties_shp@data
counties <- left_join(counties_polygon, counties_data, by=c("id"="geoid"))
@rudeboybert
rudeboybert / Lec28.R
Last active November 28, 2016 17:40
Mean age of 3 randomly chosen 116 students
library(lubridate)
library(dplyr)
library(mosaic)
set.seed(41)
students <- c(
"Annie", "Caroline", "David", "Ian", "Jack", "Jared", "Joccelyn", "Joe",
"Julia", "Luisangel", "Madeline", "Rebecca", "Samuel", "Sarah", "Sierra",
"Sophia", "Stefan", "Steven", "Theodore", "Tina", "Wengel", "William", "Zach"
)
@rudeboybert
rudeboybert / splines.R
Last active January 18, 2023 02:18
Intro to Splines
# This is the source code of Albert Y. Kim (rudeboybert)'s "Introduction to
# Splines" video.
# Lines 4-86 were run before the video and hidden from the viewer in order to
# simplify the content of the video.
library(tidyverse)
library(broom)
# Set options, random number generator seed value, & ggplot geom() + theme()
# arguments
@rudeboybert
rudeboybert / bechdel_disagree.R
Created September 27, 2017 14:18
Studying disagreement in Bechdel test evaluations
library(fivethirtyeight)
library(tidyverse)
# I still can't use forcats package from scratch
levels <- c("nowomen-disagree", "notalk-disagree", "men-disagree",
"dubious-disagree", "ok-disagree")
bechdel %>%
filter(str_detect(test, "disagree")) %>%
select(test, binary) %>%
@rudeboybert
rudeboybert / gapminder.R
Last active February 8, 2018 00:31
One Simple, One Fancy, and One Interactive gapminder Bubbleplot
library(ggplot2)
library(dplyr)
library(gapminder)
# Only for 2007 data
gapminder2007 <- gapminder %>%
filter(year == 2007)
# Simple plot -------------------------------------------------------------