This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) %>% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
NewerOlder