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:6048590
Last active December 20, 2015 01:19
R scatter plot matrix (Fair weather fans?)
# ######################
#
# Blog with output and discussion:
# "Fair weather fans? (An R scatter plot matrix)" 2013-07-18
# http://bayesball.blogspot.ca/2013/07/fair-weather-fans-r-scatter-plot-matrix.html
#
# data: pulled from www.harbourcats.com
# saved on Google Drive:
# https://docs.google.com/spreadsheet/ccc?key=0Art4wpcrwqkBdHZvTUFzOUo5U3BzMHFveXdYOTdTWUE&usp=sharing
# File / Download as > Comma Separated Values (CSV)
@MonkmanMH
MonkmanMH / HarbourCats_attendance_2013
Created August 25, 2013 17:19
HarbourCats_attendance_2013
num,date,day,day2,day.night,vs,attend,cloud,sun,temp.c,temp.f,wind,note
1,6/5/2013,Wed,1,1,Kelowna,3026,mainly sunny,4,21,70,,Opening Night
2,6/6/2013,Thu,1,1,Kelowna,1082,mainly sunny,4,18,64,,
3,6/7/2013,Fri,3,1,Kelowna,1542,mainly sunny,4,19,66,windy,
4,6/11/2013,Tue,1,1,Medford,1014,mostly cloudy,2,17,63,,
5,6/12/2013,Wed,1,1,Medford,1003,mostly cloudy,2,16,60,,
6,6/13/2013,Thu,1,1,Medford,1015,partly cloudy,3,19,66,,
7,6/21/2013,Fri,3,1,Bend,1248,sunny,5,18,64,,
8,6/22/2013,Sat,3,1,Bend,1640,sunny,5,21,70,,
9,6/23/2013,Sun,2,0,Bend,1246,cloudy,1,18,64,,

Using Google Maps API and R

[source: http://www.r-bloggers.com/using-google-maps-api-and-r/] [address modifications added by MonkmanMH]

This script uses RCurl and RJSONIO to download data from Google's API to get the latitude, longitude, location type, and formatted address

library(RCurl)
@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 / 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:5711584
Created June 5, 2013 04:21
MLB runs per game (Lahman database)
# load the package and data set "Teams"
install.packages("Lahman")
library("Lahman")
data(Teams)
#
#
# CREATE LEAGUE SUMMARY TABLES
# ============================
#
# select a sub-set of teams from 1901 [the establishment of the American League] forward to 2012
@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 / 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 / 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"))