Skip to content

Instantly share code, notes, and snippets.

View trinker's full-sized avatar

Tyler Rinker trinker

View GitHub Profile
tell_it_like_it_is <- function(){
message <- paste0(
sprintf(cow, sample(mess, 1)),
"\n\n\n\n"
)
cat(message)
}
cow <- "\n ---------------- \n%s! \n ----------------- \n \\ ^__^ \n \\ (oo)\\ ________ \n (__)\\ )\\ /\\ \n ||------w|\n || ||"
@trinker
trinker / guttenberg.R
Last active October 12, 2018 11:44
R Guttenberg API
## Originally taken from: https://juliasilge.github.io/ibm-ai-day/slides.html#1
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, gutenbergr)
## Scrape known books
titles <- c(
"Twenty Thousand Leagues under the Sea",
"The War of the Worlds",
"Pride and Prejudice",
@trinker
trinker / table_list_excel.R
Last active October 9, 2018 15:00
Write out a bunch of tables to an excel file pipeable workflow
if (!require("pacman")) install.packages("pacman"); library(pacman)
p_load(tidyverse, openxlsx, magrittr, pander, numform)
## make an environment to store everything
my_tables <- new.env()
## basic boiler plate chunk to add
## %T>%
@trinker
trinker / ggimage_demo.R
Created October 4, 2018 12:38
Plot images as points
if (!require("pacman")) install.packages("pacman")
pacman::p_load(ggimage)
presidential_icons <- data.frame(
x = c(15, 30),
y = c(2.5, 4.5),
name = c('trump', 'obama'),
image = c(
'http://www.stickpng.com/assets/images/5841c17aa6515b1e0ad75aa1.png',
@trinker
trinker / Mutlistep Cleaning Regex: Substitution & Extract Portion Before Separator.R
Created June 25, 2018 13:26
Mutlistep Cleaning/Regex: Substitution & Extract Portion Before Separator [on hold]
library(tidyverse)
as.data.frame(M, stringsAsFactors = FALSE) %>%
rownames_to_column('id') %>%
mutate(
id = gsub('SuperSMART_', 'S', id),
id = gsub('(^S)(\\d{2})(_)', '\\10\\2\\3', id, perl = TRUE)
) %>%
separate(id, into = c('S', 'R', 'p'), sep = '_', remove = FALSE) %>%
mutate(., group = group_indices(., S))
@trinker
trinker / gradient_shading.txt
Created June 25, 2018 12:04
gradient shading
https://twitter.com/lenkiefer/status/1010880251788955648
@trinker
trinker / percentiles.R
Created June 22, 2018 12:36
Computing percentiles
percentile1 <- function(x) ecdf(x)(x)
percentile2 <- function(x) rank(x)/length(x)
library(tidyverse)
out <- data_frame(
x = rnorm(100000),
ecdf = ecdf(x)(x),
rank_len = rank(x)/length(x),
same = near(ecdf, rank_len)
@trinker
trinker / speed_up_video.txt
Created May 1, 2018 15:01
ffmpeg code to speed up a video
ffmpeg -i intro_to_r.mp4 -filter_complex "[0:v]setpts=0.6666667*PTS[v];[0:a]atempo=1.5[a]" -map "[v]" -map "[a]" intro_to_r2.mp4
## r code
## atempo=1.25
## setpts=1/atempo
ffmpeg -i 04_viz_grammar_and_theory.mp4 -filter_complex "[0:v]setpts=0.8*PTS[v];[0:a]atempo=1.25[a]" -map "[v]" -map "[a]" 04_viz_grammar_and_theory2.mp4
@trinker
trinker / qqplots.R
Created April 6, 2018 00:21
QQplots explained
## My understanding of a qqplot was it was the sorted values for a variable on the
## y against the theoretical values from a normal (or whatever distribution)
## distribution on the x. We can get the y values from our sample and the x values
## from looking up the p value in a given distribution and getting the
## corresponding nromal value. Now to get the p value I thought we divide the 1
## through length of the sample by n + 1. I have never actually tried to do this
## and used qqplot or the same geom_qq from ggplot. But today I tried to make the
## qqplot by hand and I don't get the same scale for the theoretical values and am
## unsure why. The points look the same but the scale is different. Why?
@trinker
trinker / embed_youtube_start_stop_time.hrml
Created April 5, 2018 16:17
Embed a youtube video at a specific start/stop time
<!-- http://www.brucelawson.co.uk/2010/a-minimal-html5-document/ -->
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>blah</title>
</head>
<body>