Skip to content

Instantly share code, notes, and snippets.

View schochastics's full-sized avatar
👨‍🍼

David Schoch schochastics

👨‍🍼
View GitHub Profile
@schochastics
schochastics / prime_viz.R
Last active August 20, 2020 10:14
Visualization of Prime Factorization
# Recreation of https://twitter.com/stevenstrogatz/status/1295915404574040065/photo/1
library(tidyverse)
library(Polychrome)
fun <- function(x){
n <- c()
i <- 2
r <- x
while(prod(n)!=x){
if(!r%%i) {n=c(n,i);r=r/i;i=1}
i <- i+1
@schochastics
schochastics / time_prediction.R
Created November 12, 2019 18:56
time prediction
library(tidyverse)
library(ggforce)
d <- tibble(x = c(1, 6, 10, 23,18), xpred = c(4, 8, 11, 2,22), y = 1:5, id = factor(1:5))
n <- nrow(d)+1
time_angle <- tibble(hour=1:24,angle=0.25*60*hour)
d$start <- time_angle$angle[d$x]*pi/180
d$end <- time_angle$angle[d$xpred]*pi/180
d$r <- d$y
@schochastics
schochastics / lissajous.R
Last active October 22, 2019 19:47
animated lissajous curves
lissajou <- function(a,b,delta){
t <- seq(0,2*pi,0.01)
x <- sin(a*t+delta)
y <- sin(b*t)
tibble(x,y)
}
# setup parameters ----
n <- 7
params <- tibble(a=sample(1:10,n,replace = FALSE),
@schochastics
schochastics / ggpyramid.R
Created May 30, 2019 14:15
ugly 3D pyramid plot
pyramid <- function(ymax=5,mcol="#CD3333",xoffset=0){
f <- function(a,b,y){
(y-b)/a
}
a1 <- ymax/0.65
b1 <- 0
c1 <- -ymax/(1-0.65)
d1 <- -c1
a2 <- -ymax/(1-0.65)
@schochastics
schochastics / nba_scorer.R
Created April 10, 2019 17:58
Barchart Race of Top NBA Scorers
library(rvest)
library(tidyverse)
library(gganimate)
library(extrafont)
loadfonts()
get_season <- function(x){
url <- paste0("https://www.basketball-reference.com/leagues/NBA_",x,"_totals.html")
url %>%
read_html() %>%
@schochastics
schochastics / the_matrix.R
Created April 1, 2019 10:38
the matrix with gganimate
library(tidyverse)
library(gganimate)
ascii <- c(";", ":", "!", "?", ".", "'", "\"", "(", ")", "[", "]", "{",
"}", "@", "*", "/", "\\", "&", "#", "%", "`", "^", "+", "<",
"=", ">", "|", "~", "$", "0", "1", "1", "10", "2", "2", "3",
"3", "4", "4", "5", "5", "6", "6", "7", "7", "8", "8", "9", "9",
"a", "a", "A", "b", "b", "B", "c", "c", "C", "d", "d", "D", "e",
"e", "E", "f", "f", "F", "g", "g", "G", "h", "h", "H", "i", "i",
"I", "j", "j", "J", "k", "k", "K", "l", "l", "L", "m", "m", "M",
"n", "n", "N", "o", "o", "O", "p", "p", "P", "q", "q", "Q", "r",
@schochastics
schochastics / formations.csv
Created February 25, 2019 23:04
Premier league formation
id home away
1 3-2-2-2-1 4-2-2-1-1
1 P. Cech Vardy
1 Holding Okazaki
1 Monreal Albrighton
1 Kolasinac Mahrez
1 Elneny Ndidi
1 G. Xhaka M. James
1 Bellerin Fuchs
1 Oxlade-Chamberlain Maguire
@schochastics
schochastics / radial_grp_layout.R
Last active February 8, 2019 13:56
radial layout with groups
library(igraph)
library(smglr)
library(tidyverse)
library(ggraph)
g <- sample_islands(9,40,0.2,10)
g <- simplify(g)
V(g)$grp <- rep(1:9,each=40)
el <- get.edgelist(g)
E(g)$col <- case_when(
V(g)$grp[el[,1]]==1 & V(g)$grp[el[,2]] == 1 ~ 1,
library(rvest)
library(tidyverse)
# helper functions
get_theo_refs_cache <- function(url){
doc <- read_html(url)
caps <- doc %>% html_nodes("caption") %>% html_text()
skippy <- (any(caps=="Hypothesis" | caps=="Hypotheses"))+0
refs <- doc %>%
html_table() %>%
@schochastics
schochastics / get_squads.R
Created September 30, 2018 20:56
get squads from footballsquads.co.uk
library(rvest)
library(tidyverse)
leagues <- "http://www.footballsquads.co.uk/archive.htm" %>%
read_html() %>%
html_nodes("a") %>%
html_attr("href")
leagues_tbl <- as_tibble(str_split(leagues,"/",simplify = T)) %>%
mutate(link=leagues) %>%