Skip to content

Instantly share code, notes, and snippets.

@expersso
expersso / reporters_wo_borders.R
Created April 26, 2018 10:44
Slope chart for Reporters Without Borders Press Freedom Index
library(tidyverse)
df <-
"https://rsf.org/sites/default/files/import_good_-_index_2018_pour_import.csv" %>%
read_csv(locale = locale(decimal_mark = ",")) %>%
select(`Overall Score 2016`, `Score 2017`, ISO, "country" = EN_country) %>%
mutate(d = `Score 2017` - `Overall Score 2016`) %>%
gather(year, value, -ISO, -country, -d) %>%
mutate(
year = as.numeric(gsub("[^0-9]", "", year)),
@expersso
expersso / accumulate_until.R
Last active October 4, 2017 09:46
Accumulate function that accepts a predicate for when to stop
# inspiration: https://twitter.com/MilesMcBain/status/915448555027828737
accumulate_until <- function(.x, .f, .p, ..., .init = NULL) {
n <- length(.x)
m <- mode(.x)
out <- vector(m, n)
if(!is.null(.init)) {
.x <- c(.init, .x)
}
@expersso
expersso / unnest.R
Created September 7, 2017 13:09
Unnesting lists with dplyr
# Motivation: https://twitter.com/zentree/status/905666552925536256
library(tidyverse)
library(rlang)
unnest_dfc <- function(df, var) {
var <- enquo(var)
new_df <- df %>%
pull(!!var) %>%
transpose() %>%
@expersso
expersso / anagrams.R
Created June 16, 2017 12:53
Fundamental theorem of arithmetic to determine anagrams
# Uses Fundamental Theorem of Arithmetic to determine if
# two character strings are anagrams of each other.
#
# Inspired by:
# https://www.reddit.com/r/math/comments/6hb0xk/
# clever_algorithm_to_determine_whether_or_not_two/
library(primes)
library(purrr)
@expersso
expersso / rogoff.R
Last active June 13, 2017 12:08
Chart on banking crises using Reinhart, Rogoff (2009) data
library(tidyverse)
library(scales)
library(readxl)
library(forcats)
get_data <- function() {
tmp <- tempfile(fileext = ".xlsx")
on.exit(unlink(tmp))
download.file("http://www.carmenreinhart.com/user_uploads/data/124_data.xlsx",
tmp, mode = "wb")
@expersso
expersso / nim.R
Last active June 13, 2017 12:10
Implementation of Nim in R
## Nim
next_player <- function(player) if(player == 1) 2 else 1
valid_move <- function(board, row, num) board[row] >= num
finished <- function(board) all(board == 0)
update <- function(board, row, num) `[<-`(board, row, board[row] - num)
disp_row <- function(row, num) cat(row, ":", rep("*", num), "\n")
disp_board <- function(board) purrr::walk2(seq_along(board), board, disp_row)
nim <- function(board = 5:1, player = 1) {
if(finished(board)) {
@expersso
expersso / pig_latin.R
Created June 2, 2017 15:14
Pig latin translator
library(stringr)
library(purrr)
vowels <- c("a", "e", "i", "o", "u")
is_vowel <- function(l) l %in% c(vowels, toupper(vowels))
is_capitalized <- . %>% substr(1, 1) %>% str_detect("[[:upper:]]")
split_at <- function(x, i) split(x, cumsum(seq_along(x) %in% i))
pigify_word <- function(word) {
ltrs <- strsplit(word, "")[[1]]
@expersso
expersso / structural_indices.R
Created March 14, 2017 16:39
Timeline of structural indices
# Based on https://twitter.com/andrewheiss/status/841655403087822848
library(tidyverse)
url <- paste0("https://gist.githubusercontent.com/andrewheiss/",
"db6c981f1032207fd5a1d1a59113c469/raw/",
"23768ef3d9dc87a53efd891da4d3d1a448dbca34/long.csv")
df <- read.csv(url, stringsAsFactors = FALSE) %>%
tbl_df() %>%
@expersso
expersso / pascal.R
Last active March 2, 2017 16:06
Generate Pascal's triangle using purrr
library(tidyverse)
library(stringr)
pascal_triangle <- function(n) {
df <- data_frame(N = 0:n, k = map(N, seq, from = 0)) %>%
unnest() %>%
mutate(c = choose(N, k)) %>%
group_by(N) %>%
summarise(c = paste(c, collapse = " "))
@expersso
expersso / FT_health_spending.R
Created January 27, 2017 14:10
Recreating FT chart on health spending and life expectancy in OECD countries
library(tidyverse)
library(scales)
library(OECD)
library(grid)
library(gridExtra)
oecd <- c("AUS","AUT","BEL","CAN","CHL","CZE","DNK","EST","FIN","FRA",
"DEU","GRC","HUN","ISL","IRL","ISR","ITA","JPN","KOR","LVA",
"LUX","MEX","NLD","NZL","NOR","POL","PRT","SVK","SVN","ESP",
"SWE","CHE","TUR","GBR","USA")