Skip to content

Instantly share code, notes, and snippets.

View k5cents's full-sized avatar

Kiernan Nicholls k5cents

View GitHub Profile
@k5cents
k5cents / owgr_pop.R
Created July 17, 2023 15:36
Top Golfers Per Capita (by Gender)
library(tidyverse)
library(janitor)
library(httr)
library(k5)
# mens owgr ---------------------------------------------------------------
a <- GET(
url = "https://apiweb.owgr.com/api/owgr/rankings/getRankings",
query = list(
@k5cents
k5cents / mil_spend.R
Created March 10, 2022 01:47
Military equipment comparison
library(tidyverse)
library(janitor)
library(scales)
library(rvest)
x <- read_html("https://en.wikipedia.org/wiki/List_of_countries_by_level_of_military_equipment")
clean_wiki <- function(x) {
x %>%
str_remove_all("\\[(.*)\\]") %>%
@k5cents
k5cents / spotify_plot.R
Last active February 23, 2024 00:19
Create some plots from Spotify streaming history data
library(tidyverse)
library(jsonlite)
library(scales)
library(fs)
library(k5)
# functions ---------------------------------------------------------------
most_common <- function (x, n = 6) {
as.vector(na.omit(names(sort(table(x), decreasing = TRUE)[1:n])))
@k5cents
k5cents / ipsos_music_aug.R
Created September 1, 2021 16:55
Plotting Ipsos' Understanding Society survey on music preference by generation
library(tidyverse)
library(tabulizer)
library(janitor)
library(scales)
library(fs)
ipsos_pdf <- file_temp(ext = "pdf")
download.file(
"https://www.ipsos.com/sites/default/files/ct/news/documents/2021-09/Understanding%20Society%20Wave%2018%20Topline_083121.pdf",
destfile = ipsos_pdf
@k5cents
k5cents / date-seq.R
Last active June 1, 2021 21:50
Determine certain dates in a sequence
library(lubridate)
x <- seq(from = mdy("January 23, 1845"), to = today(), by = "1 day")
x[year(x) %% 4 == 0 & month(x) == 11 & day(x) <= 7 & wday(x) == 2] + 1
@k5cents
k5cents / 90-blacklist-webcam-sound.rules
Last active October 18, 2021 19:35
Disable webcam audio and preserve video
# Disables webcam audio.
SUBSYSTEM=="usb", DRIVER=="snd-usb-audio", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="0991", ATTR{authorized}="0"
@k5cents
k5cents / med_county_letter.R
Last active February 4, 2021 18:02
Finding the first letter of each state's median county
# install.packages("usa")
library(tidyverse)
x <- usa::counties %>%
filter(state %in% state.abb) %>%
group_by(state) %>%
arrange(name, .by_group = TRUE) %>%
mutate(first_letter = str_sub(name, end = 1)) %>%
filter(first_letter == first_letter[length(first_letter)/2]) %>%
@k5cents
k5cents / sen_pop.R
Created November 15, 2020 22:05
Calculate proportion of population represented by Senate majority party over time
library(tidyverse)
library(lubridate)
library(jsonlite)
library(janitor)
library(rvest)
# state pop history -------------------------------------------------------
# look for zip export from stl fed
# zip has 2016-2019, not on wikipedia
@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")