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:4720641
Last active December 12, 2015 05:18
MLB runs per game per team
# THE HISTORICAL RECORD - RUNS PER GAME BY TEAM
#
# public Gist of this code can be found at
# https://gist.github.com/MonkmanMH/4720641
#
# data source: Lahman Database
# http://www.seanlahman.com/baseball-archive/statistics/
# 2012 version (1871-2012)
# data table "Teams.csv"
#
@MonkmanMH
MonkmanMH / gist:4969818
Last active December 13, 2015 20:28
MLB team runs per game
# INDIVIDUAL TEAM HISTORY
#
# discussion and output can be found at
# http://bayesball.blogspot.ca/XXXXXXXXXXXXXXXXXXXXX
#
# Select the team you want from the franchID variable in the Teams.merge data frame
# and create a new data frame called "Team1"
# For this exercise we will use the Seattle Mariners, which are coded as SEA
# Note the use of double "=" to define the team!
Team1 <- as.data.frame(subset (Teams.merge, franchID == "SEA"))
@MonkmanMH
MonkmanMH / gist:4970480
Created February 17, 2013 06:45
MLB runs per game - league trends
# MAJOR LEAGUE BASEBALL - RUNS PER GAME TREND
#
# discussion at
# 1. http://bayesball.blogspot.ca/2012/07/trends-in-al-run-scoring-using-r.html
# 2. http://bayesball.blogspot.ca/2012/07/trends-in-run-scoring-nl-edition-more-r.html
# 3. http://bayesball.blogspot.ca/2012/08/trends-in-run-scoring-comparing-leagues.html
#
# data source: Baseball Reference
# http://www.baseball-reference.com
# http://www.baseball-reference.com/leagues/AL/bat.shtml
@MonkmanMH
MonkmanMH / gist:5027789
Created February 25, 2013 04:43
MLB runs per game - league average
# THE HISTORICAL RECORD - RUNS PER GAME
#
# discussion and output can be found at
# http://bayesball.blogspot.ca/2013/02/comparing-individual-team-run-production.html
#
# data source: Lahman Database
# http://www.seanlahman.com/baseball-archive/statistics/
# 2012 version (1871-2012)
# table: "Teams"
#
@MonkmanMH
MonkmanMH / gist:5027793
Created February 25, 2013 04:44
MLB team history - runs allowed
# INDIVIDUAL TEAM HISTORY - RUNS ALLOWED
#
# select the team you want from the franchID variable in the Teams.merge data frame
# and create a new data frame called "Team1"
# note the use of double "=" to define the team!
Team1 <- as.data.frame(subset (Teams.merge, franchID == "SEA"))
#
# create what will be the chart title from the contents of Team1
# note that teams sprang into existence in different years, thus the requirement to define both the start and end dates
firstyear <- Team1$yearID[1]
@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 / gist:5802497
Created June 18, 2013 03:30
Annotating select points on an X-Y plot using ggplot2
#
# for details see
# http://bayesball.blogspot.ca/2013/06/annotating-select-points-on-x-y-plot.html
#
# load the ggplot2 and grid packages
library(ggplot2)
library(grid)
# read data (note csv files are renamed)
tbl1 = read.csv("FanGraphs_Leaderboard_h.csv")
tbl2 = read.csv("FanGraphs_Leaderboard_d.csv")
@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)