Skip to content

Instantly share code, notes, and snippets.

View dgrtwo's full-sized avatar

David Robinson dgrtwo

View GitHub Profile
@dgrtwo
dgrtwo / simulate-dice
Last active January 5, 2020 16:32
Simulate scoring games of Dice 10,000
# 100,000 simulations
n <- 1e5
dice <- t(rmultinom(n, 5, rep(1, 6) / 6))
# First rules for 1s and 5s
ones_score <- c(0, 100, 200, 1000, 5000, 10000)[dice[, 1] + 1L]
fives_score <- c(0, 50, 100, 0, 0, 0)[dice[, 5] + 1L]
# 100 x n score
most_common <- apply(dice, 1, which.max)
@dgrtwo
dgrtwo / dice-rolls.R
Created February 25, 2019 23:56
Animation of die rolls
# Code behind this tweet: https://twitter.com/drob/status/1100182329350336513
library(tidyverse)
library(gganimate)
# Setup
options(gganimate.nframes = 200)
set.seed(2019)
simulation <- tibble(roll = 1:10000) %>%
mutate(result = sample(6, n(), replace = TRUE)) %>%
# Code behind this tweet:
# https://twitter.com/drob/status/1126988304090574848
library(tidyverse)
library(broom)
t_tests <- crossing(pi0 = .75,
effect_size = .25,
trial = 1:10000,
@dgrtwo
dgrtwo / saveRDS.Rmd
Last active September 3, 2018 19:08
saveRDS speed and size
---
title: "Effect of compression type and file complexity on saveRDS size and speed"
author: "David Robinson"
date: "April 20, 2015"
output: html_document
---
```{r echo = FALSE}
knitr::opts_chunk$set(cache = TRUE, message = FALSE)
```
@dgrtwo
dgrtwo / gist:39af4512dff5b7357b534a04a971405d
Created March 14, 2018 22:21
Early attempt at creating materialized SQL views from dplyr
separate_sql <- function(expression) {
s <- paste(deparse(expression), collapse = "\n")
s <- stringr::str_replace(s, "%>%", "%>%\n ")
s <- stringr::str_split(s, "\n")[[1]]
val <- eval(expression)
list(expression = expression,
dplyr_code = s,
sql = as.character(dbplyr:::remote_query(val)),
@dgrtwo
dgrtwo / keybase.md
Last active February 8, 2018 15:57

Keybase proof

I hereby claim:

  • I am dgrtwo on github.
  • I am drobinson (https://keybase.io/drobinson) on keybase.
  • I have a public key whose fingerprint is 2BFD FAC9 CDA9 669F FE2C 6973 C0B7 A65C CA30 6675

To claim this, I am signing this object:

@dgrtwo
dgrtwo / barcode_splitter.py
Created September 15, 2012 00:21
Barcode splitter for fastq sequencing files that splits using Levenshtein distance.
"""
barcode_splitter.py
Barcode splitter for fastq sequencing files, that matches using Levenshtein
distance.
USAGE:
python barcode_splitter.py reads.fastq index_reads.fastq barcodes.txt
library(purrr)
transition_mc <- function(steps, start, mat) {
i <- seq_len(nrow(mat))
transition <- ~ sample(i, 1, prob = (i == .) %*% mat)
accumulate(seq_len(steps), transition, .init = start)
}
---
title: "R Notebook"
output: html_notebook
---
```{r}
library(purrr)
transition_mc <- function(steps, start, mat) {
i <- seq_len(nrow(mat))
library(ggplot2)
library(igraph)
library(ggraph)
library(scales)
library(ggforce)
network_theme <- theme_no_axes() +
theme(panel.border = element_blank())
theme_set(network_theme)