Skip to content

Instantly share code, notes, and snippets.

View MonkmanMH's full-sized avatar
🎯
Multi-tasking

Martin Monkman MonkmanMH

🎯
Multi-tasking
View GitHub Profile
### ---
#
# from @expersso
set.seed(894) # number of regular season NHL goals Wayne Gretzky scored
x <- replicate(10000, sum(sample(0:1, 20, TRUE, c(0.945, 0.055))))
table(ifelse(x == 0, "Team A win", ifelse(x == 1, "Draw", "Team B win"))) / 100
@MonkmanMH
MonkmanMH / correlatedrandomvariables.r
Last active October 20, 2017 22:31
correlated random variables
## make it reproducible
set.seed(8675309)
## two normally distributed variables
X1 <- rnorm(100, mean = 0, sd = 1)
X2 <- rnorm(100, mean = 0, sd = 1)
@MonkmanMH
MonkmanMH / dummy_var.R
Last active October 18, 2017 18:51
quick
# quick example of mutate (in the dplyr R package) to create a dummy variable
# packages (from the tidyverse)
library(tibble)
library(dplyr)
# a little tibble with an ID number and a gender variable (5 Female, 3 Male, 2 Not Stated)
mydata <- tibble(id = 1:10, gender = c("F", "F", "F", "F", "F",
"M", "M", "M",
"NS", "NS"))
@MonkmanMH
MonkmanMH / mutate_alternate.r
Created July 5, 2017 02:40
mutate alternate values
library(tidyverse)
datatab <- as.tibble(c(1:10))
# modulo division
datatab$value %% 2
# since we have alternating even and odd value in "value" variable
datatab %>%
mutate(valueplus = ifelse((value %% 2) == 0, "even", "odd"))
@MonkmanMH
MonkmanMH / col_name.r
Created July 5, 2017 02:04
column naming loop
# Problem:
# - row 2 of data file has non-data title that repeats every two columns
# - column 1 / row 1 header label is fine
# - the header in every even-numbered column applies to the next odd-humbered column (eg 2 applies to 3, 4 to 5, etc)
# - the header in those odd-numbered columns (3, 5, 7, etc) is read initially as an NA
# Solution
# - read column names only
# - hard code even and odd suffix
# - copy header value in those even columns to odd columns
@MonkmanMH
MonkmanMH / datefixLahman.R
Created January 6, 2016 05:11
Work-around quick fix for inconsistent date values in the Master table of the Lahman package (R)
#
library(Lahman)
data(Master)
#
# `debut` variable; create new version `debutDate`
Master$debutDate <- (as.Date(Master$debut, "%m/%d/%Y"))
Master$debutDate[is.na(Master$debutDate)] <-
as.Date(Master$debut[is.na(Master$debutDate)])
#
# `finalGame` variable; create new version `finalGameDate`
@MonkmanMH
MonkmanMH / gist:efdf9c772054131ca22f
Last active August 29, 2015 14:05
Lahman 3.0 tests
---
title: "Testing Lahman 3.0"
author: "Martin Monkman"
date: "Sunday, August 31, 2014"
output: html_document
---
This markdown document incorporates a variety of short scripts that draw on the various tables in the `Lahman` package. (See the Lahman project page on RForge for more details <http://lahman.r-forge.r-project.org/>.)
Note that some of scripts appear in the documentation of other R packages; in those cases, the original source is noted prior to the script.
@MonkmanMH
MonkmanMH / gist:0f92cba504f2e7f11bba
Created July 29, 2014 03:13
Wes Anderson palette in R
if (!require(wesanderson)) install.packages("wesanderson")
library(wesanderson)
# for more on the Wes Anderson colour palette:
# https://github.com/karthik/wesanderson#wes-anderson-palettes
# http://blog.revolutionanalytics.com/2014/03/give-your-r-charts-that-wes-anderson-style.html
#
#
#
# add some Wes Anderson "Grand Budapest Hotel" colour to print object "p2"
p2 + scale_fill_manual(values = wes.palette(4, "GrandBudapest"))
@MonkmanMH
MonkmanMH / gist:3c0da6afd58eb61e2c51
Last active August 29, 2015 14:04
dplyr testing and goofing
#
# setwd("D:/R_the software/datatrials/Lahman")
#
require(Lahman)
require(dplyr)
#
# throwing by position
# version 1 - "merge"
MasterFielding <- data.frame(merge(Master, Fielding, by="playerID"))
MasterFielding <- merge(Master, Fielding, by="playerID")