Skip to content

Instantly share code, notes, and snippets.

@bdilday
bdilday / grstand1.R
Created Aug 24, 2020
graphical mlb standings in R
View grstand1.R
library(dplyr)
library(ggplot2)
library(ggrepel)
library(rvest)
library(stringr)
fg_current_standings <- function() {
url = "https://www.fangraphs.com/depthcharts.aspx?position=Standings"
View mlb_stadium.json
[{"team":"Anaheim Angels","address":"2000 Gene Autry Way, Anaheim, CA. 92806","lat":33.799572,"lng":-117.889031},{"team":"Arizona Diamondbacks","address":"P.O. Box 2095, Phoenix, AZ. 85001","lat":33.452922,"lng":-112.038669},{"team":"Atlanta Braves","address":"P.O. Box 4064, Atlanta, GA. 30302","lat":33.74691,"lng":-84.391239},{"team":"Baltimore Orioles","address":"333 W. Camden Street, Baltimore, MD. 21201","lat":39.285243,"lng":-76.620103},{"team":"Boston Red Sox","address":"4 Yawkey Way, Boston, MA 02215","lat":42.346613,"lng":-71.098817},{"team":"Chicago Cubs","address":"1060 Addison Street, Chicago, IL 60616","lat":41.947201,"lng":-87.656413},{"team":"Chicago White Sox","address":"333 W. 35th Street, Chicago, IL 60616","lat":41.830883,"lng":-87.635083},{"team":"Cincinnati Reds","address":"100 Cinergy Field, Cincinnati, OH 45202","lat":39.107183,"lng":-84.507713},{"team":"Cleveland Indians","address":"2401 Ontario Street, Cleveland, OH 44115","lat":41.495149,"lng":-81.68709},{"team":"Colorado Rockies","ad
@bdilday
bdilday / ncaa_mbb_current_results_2019.csv
Last active Mar 30, 2019
ncaa march madness results 2019
View ncaa_mbb_current_results_2019.csv
game result
2019_1242_1318 1
2019_1266_1293 0
2019_1233_1314 0
2019_1330_1345 0
2019_1199_1436 1
2019_1276_1285 1
2019_1124_1393 1
2019_1279_1328 0
2019_1125_1396 1
@bdilday
bdilday / war_data.csv
Last active Sep 2, 2018
war_data.csv
View war_data.csv
We can't make this file beautiful and searchable because it's too large.
"playerID","yearID","WAA","WAR","WAR_def","WAR_off","POS"
"aaronha01",1954,-0.61,1.27,-0.71,1.98,"OF"
"aaronha01",1955,3.77,6.25,-0.01,6.26,"OF"
"aaronha01",1956,4.63,7.18,0.65,6.53,"OF"
"aaronha01",1957,5.5,7.97,-0.13,8.1,"OF"
"aaronha01",1958,4.84,7.32,0.21,7.11,"OF"
"aaronha01",1959,6.13,8.64,-1.08,9.72,"OF"
"aaronha01",1960,5.47,7.98,0.77,7.21,"OF"
"aaronha01",1961,6.86,9.4,2.02,7.38,"OF"
@bdilday
bdilday / catcher_defense_data.csv
Last active Sep 1, 2018
catcher_defense_data.csv
View catcher_defense_data.csv
We can't make this file beautiful and searchable because it's too large.
"game_id","year_id","bat_home_id","off_score","def_pitcher","def_catcher","off_team","def_team","park_id","park"
"WS2196109210",1961,0,3,"danib102_1961","retzk101_1961","MIN_1961","WS2_1961","WS2_1961","WS2"
"WS2196109190",1961,0,2,"mcclj104_1961","retzk101_1961","MIN_1961","WS2_1961","WS2_1961","WS2"
"WS2196109150",1961,0,1,"danib102_1961","retzk101_1961","KC1_1961","WS2_1961","WS2_1961","WS2"
"WS2196109130",1961,0,1,"hobae101_1961","retzk101_1961","BOS_1961","WS2_1961","WS2_1961","WS2"
"WS2196109120",1961,0,0,"burnp102_1961","retzk101_1961","BOS_1961","WS2_1961","WS2_1961","WS2"
"WS2196109032",1961,0,2,"gablg102_1961","dalep101_1961","CHA_1961","WS2_1961","WS2_1961","WS2"
"WS2196109170",1961,0,2,"maesh101_1961","retzk101_1961","KC1_1961","WS2_1961","WS2_1961","WS2"
"WS2196109160",1961,0,0,"burnp102_1961","retzk101_1961","KC1_1961","WS2_1961","WS2_1961","WS2"
View mlb_stadia_paths.csv
team x y segment
angels 26.27 104.81 outfield_outer
angels 25.932060362453754 98.80779459065957 outfield_outer
angels 26.551757956401122 92.8655601538784 outfield_outer
angels 29.069970572128522 87.4573503249933 outfield_outer
angels 32.986155173212474 82.91330959433427 outfield_outer
angels 37.08065184258191 78.5115488314632 outfield_outer
angels 41.202800272760506 74.13565416464868 outfield_outer
angels 45.356441744254404 69.7896729842234 outfield_outer
angels 49.49117542835149 65.42575404594743 outfield_outer
View statcast_impute_derivation.R
library(baseballr)
library(dplyr)
obtain_data = function(type="postgres",
start_date="2017-03-29",
end_date="2017-10-03", infile=NULL) {
if (type=="postgres") {
# postgres db connection here
@bdilday
bdilday / scrape_statcast_expected_stats.R
Last active May 17, 2018
scrape statcast expected stats
View scrape_statcast_expected_stats.R
library(dplyr)
library(ggplot2)
library(rvest)
library(jsonlite)
scrape_statcast_expected_stats = function(year=2018, min_pa=25) {
url = sprintf("https://baseballsavant.mlb.com/expected_statistics?type=batter&year=%s&position=&team=&min=%d", year, min_pa)
h = xml2::read_html(url)
s = html_nodes(h, "script")[[10]]
@bdilday
bdilday / top10warfranch.R
Last active Apr 15, 2018
R graph - top10 war by franchise
View top10warfranch.R
library(readr)
library(ggplot2)
library(dplyr)
library(Lahman)
br_war = read_csv("https://www.baseball-reference.com/data/war_daily_bat.txt")
m = Lahman::Teams
ndf = br_war %>% merge(m %>% select(teamIDBR, yearID, name, franchID), by.x=c("team_ID", "year_ID"), by.y=c("teamIDBR", "yearID")) %>% group_by(franchID, name, player_ID) %>% summarise(w=sum(as.numeric(WAR), na.rm=TRUE))
xx = ndf %>% arrange(franchID, -w) %>% mutate(i=row_number()) %>% group_by(franchID, name) %>% summarise(m=max(i)) %>% arrange(franchID,-m) %>% mutate(ir=row_number()) %>% group_by(franchID) %>% mutate(m2=max(m), m=sum(m)) %>% filter(ir==1) %>% select(franchID, m=m2)
p = ndf %>% merge(xx, by="franchID") %>% group_by(franchID) %>% arrange(-w) %>% mutate(i=row_number()) %>% filter(i<=10) %>% filter(m>=110) %>% ggplot(aes(x=i, y=w)) + geom_bar(stat='identity') + facet_wrap(~franchID) + theme_minimal() + labs(x='franchise rank', y='career WAR')
@bdilday
bdilday / trajectory_calculator1.R
Last active Feb 20, 2018
trajectory calculator
View trajectory_calculator1.R
trajectory_pars <- list(
# constants
mass = 5.125, # oz,
circumference = 9.125, # in
beta = 1.217e-4, # 1 / meter
cd0 = 0.3008,
cdspin = 0.0292,
cl0 = 0.583,
cl1 = 2.333,