Skip to content

Instantly share code, notes, and snippets.

library(rvest)
library(purrr)
url <- 'http://www.basketball-reference.com/boxscores/201506140GSW.html'
tables <- url %>% read_html() %>% html_nodes('.sortable.stats_table')
headers <- tables %>% html_nodes(xpath = 'thead/tr[@class=""]') %>%
map(html_nodes, 'th') %>%
map(html_text)
@alistaire47
alistaire47 / keybase.md
Created October 28, 2017 02:57
keybase.md

Keybase proof

I hereby claim:

  • I am alistaire47 on github.
  • I am alistaire (https://keybase.io/alistaire) on keybase.
  • I have a public key ASCDokqb8qC26I5KvdqBatfUEG1x_I_64tywK32tK-CSrAo

To claim this, I am signing this object:

@alistaire47
alistaire47 / trump-score.R
Last active January 1, 2018 18:41
fivethirtyeight trump score
library(rvest)
library(tidyverse)
h <- read_html('https://projects.fivethirtyeight.com/congress-trump-score/house/')
trump_score <- h %>%
html_nodes('table.member-list') %>%
map(html_table, fill = TRUE) %>%
set_names(c('Senate', 'House')) %>%
map(set_names,
@alistaire47
alistaire47 / markdown.R
Last active October 18, 2018 19:41
read markdown table into R data frame
# base R version
read.markdown <- function(file, stringsAsFactors = FALSE, strip.white = TRUE, ...){
if (length(file) > 1) {
lines <- file
} else if (grepl('\n', file)) {
con <- textConnection(file)
lines <- readLines(con)
close(con)
} else {
lines <- readLines(file)
@alistaire47
alistaire47 / pythagorean_triples.Rmd
Created February 22, 2018 04:21
Pythagorean Triples
---
title: "Pythagorean Triples"
author: "Edward Visel"
output:
html_document:
df_print: paged
---
```{r}
library(tidyverse)
coalesce_join(
tribble(
~key, ~var1, ~var2,
'a', 1, NA,
'b', NA, 2
),
tribble(
~key, ~var1, ~var2,
'a', NA, 1,
'b', 2, NA
@alistaire47
alistaire47 / uptake-gif.R
Created November 28, 2018 04:36
uptake gifs
library(dplyr)
library(ggplot2)
library(gganimate)
### Horizontal rotation
p <- data_frame(x = c(-4, 0, 4), y = c(0, 3, 0), state = 1) %>%
bind_rows(mutate(., x = -x, state = 2)) %>% # frame flipped over y-axis
ggplot(aes(x, y, ymin = y, ymax = y + 1)) +
geom_ribbon() +
@alistaire47
alistaire47 / heart.R
Last active February 15, 2019 07:11
heart animation
library(tidyverse)
library(gganimate)
p <- crossing(t = (1:4)*pi/2,
x = seq(0, 2*pi, length = 501)) %>%
mutate(y = 2 - 2*sin(x) + sin(x) * sqrt(abs(cos(x)))/(sin(x) + 1.4),
x = (x + t) %% (2*pi)) %>%
ggplot(aes(x, y, color = x)) +
geom_path(size = 2, show.legend = FALSE) +
scale_color_gradient2(low = '#7a0177', mid = '#f768a1', high = '#fcc5c0') +
@alistaire47
alistaire47 / squircle.R
Created February 24, 2019 18:10
squircles
library(tidyverse)
library(gganimate)
animate(
crossing(
p = seq(0.1, 5, by = 0.1),
theta = seq(0, 2*pi, length.out = 101)
) %>%
mutate(r = 1 / (abs(cos(theta))^p + abs(sin(theta))^p)^(1/p)) %>%
ggplot(aes(theta, r, fill = p)) +
@alistaire47
alistaire47 / fugly-but-fast.R
Created August 27, 2019 01:16
brodie's challenge
alistaire <- function(n){
two_rows <- rep(seq_len(n/2L), each = 4L)
out_mat <- matrix(0L, n, n)
index_mat <- matrix(seq_len(n), nrow = 2L)
for (i in seq_len(n/2L)){
out_mat[index_mat[, i], ] <- two_rows
two_rows <- two_rows + 1L
}
out_mat
}