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
@MonkmanMH
MonkmanMH / gist:6861482
Created October 7, 2013 01:55
while() and for() loops in R
# PRINT THE INTEGERS 1 THROUGH 10
#
# VERSION 1 -- using while()
# make the initial assignment of variable count_1 to 0 (not necessary)
count_1 <- 0
# the while loop - conditional statement in the first parenthesis,
# then the repeated steps within the {}
while (count_1 < 10)
{ count_1 <- count_1 + 1
print(count_1)
@MonkmanMH
MonkmanMH / gist:6891654
Last active December 25, 2015 00:59
calculate confidence interval (binomial distribution) - function in R
# BINOMIAL CONFIDENCE INTERVAL CALCULATOR
#
# the binomial distribuion approximates the Normal distribution
# http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval
#
# read the data file
bin_data <- read.csv("bin_data.csv")
#
#
# the binomial confidence calculator function
@MonkmanMH
MonkmanMH / gist:7740998
Last active September 6, 2020 02:22
Random number generation in R (rstats, #rstats)

Random numbers in R

The creation of random numbers, or the random selection of elements in a set (or population), is an important part of statistics and data science. From simulating coin tosses to selecting potential respondents for a survey, we have a heavy reliance on random number generation.

R offers us a variety of solutions for random number generation; here's a quick overview of some of the options.

runif, rbinom, rnorm

One simple solution is to use the runif function, which generates a stated number of values between two end points (but not the end points themselves!) The function uses the continuous uniform distribution, meaning that every value between the two end points has an equal probability of being sampled.

@MonkmanMH
MonkmanMH / gist:8798762
Last active August 29, 2015 13:56
Percentile function in R
# CALCULATING PERCENTILES IN R
#
# a basic percentile function using "ecdf" [Empirical Cumulative Distribution Function]
# using a data file "percentiledata" with variable VALUE
percentileFUN <- ecdf(percentiledata$VALUE)
percentileFUN
percentileFUN(percentiledata$VALUE)
# write the percentile values to the source file
percentiledata$pctl <- percentilefunction(percentiledata$VALUE)
#
@MonkmanMH
MonkmanMH / gist:9190970
Last active December 15, 2023 03:19
Categorical data analysis in R - a resource list
@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")
@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: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 / 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`