This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() %>% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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)) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() %>% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = " ")) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |