Skip to content

Instantly share code, notes, and snippets.

View k5cents's full-sized avatar

Kiernan Nicholls k5cents

View GitHub Profile
@k5cents
k5cents / prez_turnout.R
Last active September 23, 2020 19:11
Comparing past and future Presidential election turnout
library(patchwork)
library(tidyverse)
library(lubridate)
library(jsonlite)
library(predictr)
library(scales)
library(rvest)
# get vap estimate --------------------------------------------------------
@k5cents
k5cents / min-popvote.R
Last active October 9, 2023 16:44
Calculating the minimum number of votes needed to win the 2016 Electoral College
library(tidyverse)
library(rvest)
# scrape state votes from wikipedia table
prez_wiki <- read_html("https://w.wiki/Zqi")
vote_table <- prez_wiki %>%
html_node(xpath = '//*[@id="mw-content-text"]/div[1]/div[40]/table') %>%
html_table(fill = TRUE) %>%
as_tibble(.name_repair = "unique")
@k5cents
k5cents / pi-college-margin.R
Created June 16, 2020 15:01
Prediction market prices for electoral college margins of victory
# kiernan Nicholls @kiernxn
# Tue Jun 16 10:17:55 2020 ------------------------------
# remotes::install_github("kiernann/predictr")
library(predictr)
library(tidyverse)
x <- market_price(6653, FALSE)
x$contract <- x$contract %>%
str_replace("Dems by", "D") %>%
str_replace("GOP by", "R") %>%
@k5cents
k5cents / zombie_movies.R
Created May 19, 2020 15:44
Counting zombie movies made per year
z <- read_html("https://en.wikipedia.org/wiki/List_of_zombie_films") %>%
html_node(".wikitable") %>%
html_table() %>%
as_tibble() %>%
na_if("") %>%
clean_names("snake") %>%
count(year) %>%
ggplot(aes(year, n)) +
geom_col() +
scale_x_continuous(breaks = seq(1930, 2020, by = 5)) +
@k5cents
k5cents / ubuntu-setup.txt
Last active September 29, 2021 19:19
The commands I need to set up my Ubuntu desktop
# terminal padding
# see: https://git.io/JfmQU
sudo gedit ~/.config/gtk-3.0/gtk.css
# install R 4.0
sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo apt update
sudo apt install r-base r-base-core r-recommended r-base-dev
@k5cents
k5cents / gtk.css
Last active April 24, 2020 16:56
Add 10px padding to the Gnome Terminal
VteTerminal,
TerminalScreen,
vte-terminal {
padding: 10px 10px 10px 10px;
-VteTerminal-inner-border: 10px 10px 10px 10px;
}
@k5cents
k5cents / common-eng-tld.R
Last active March 6, 2020 22:58
Common english words ending in a top-level-domain
library(tidyverse)
# read all top level domains
tlds <- str_to_lower(read_lines(
file = "http://data.iana.org/TLD/tlds-alpha-by-domain.txt",
skip = 1
))
# obtain 10000 most common eng words
words <- read_lines("https://raw.githubusercontent.com/first20hours/google-10000-english/master/google-10000-english-usa.txt")
@k5cents
k5cents / tidy_poker.R
Created February 11, 2020 21:32
Dealing and evaluating random poker hands
library(tidyverse)
rank <- c(2:9, "T", "J", "Q", "K", "A")
rank <- factor(rank, rank, ordered = TRUE)
suit <- factor(c("s", "h", "d", "c"))
deck <- expand.grid(rank = rank, suit = suit)
shuffle <- function(d) {
sample_frac(x)
}
@k5cents
k5cents / gop_convict.R
Created January 21, 2020 18:42
Proabaility of GOP senate votes to convict Trump
library(tidyverse)
library(glue)
library(httr)
library(jsonlite)
library(magrittr)
library(patchwork)
gop_data <- content(GET("https://www.predictit.org/api/marketdata/markets/6304"))
gop_contracts <- gop_data %>%
use_series(contracts) %>%
map_df(as_tibble) %>%
@k5cents
k5cents / keybase.md
Created November 13, 2019 15:11
Proving my keybase identity

Keybase proof

I hereby claim:

  • I am kiernann on github.
  • I am kiernxn (https://keybase.io/kiernxn) on keybase.
  • I have a public key ASAhsMx6AGPlWh7FmSynjb9T_NKv5-yxuk6gI0pjad8vHwo

To claim this, I am signing this object: