Skip to content

Instantly share code, notes, and snippets.

View BillPetti's full-sized avatar

Bill Petti BillPetti

View GitHub Profile
@BillPetti
BillPetti / gist:7216bd30522361705e959768f66ce5a1
Created May 30, 2021 20:31
Joining results table and urls where there are no common keys to use
library(tidyverse)
# first table of game results, where some games where not played
table_1 <- tibble(game = seq(1,10,1),
result = c('W', 'W', "Ppd",
"W", "W", "W",
"Ppd", "W", "W",
"Ppd"))
@BillPetti
BillPetti / max_ecdf_dist.R
Last active February 4, 2019 20:28
A function for calculating the value for which the difference in ecdf is largest for two groups
# df is a dataframe
# group_var is the variable that contains your two groups that you want to compare--think 1 vs. 0
# feature_var is the feature or variable whose values you are interested in exploring
# this function calculates the value with the max difference and returns that only
max_ecdf <- function(df, group_var, feature_var) {
var <- enquo(group_var)
feature_enquo <- enquo(feature_var)
@BillPetti
BillPetti / format_old_savant_output.R
Last active December 16, 2018 03:36
Function for transforming old Baseball Savant file exports so they can be merged with the new file exports
format_old_savant_output <- function(df) {
updated_names <- c("pitch_type", "pitch_id", "game_date", "release_speed", "release_pos_x", "release_pos_z", "player_name", "batter", "pitcher", "events", "description", "spin_dir", "spin_rate_deprecated", "break_angle_deprecated", "break_length_deprecated", "zone", "des", "game_type", "stand", "p_throws", "home_team", "away_team", "type", "hit_location", "bb_type", "balls", "strikes", "game_year", "pfx_x", "pfx_z", "plate_x", "plate_z", "on_3b", "on_2b", "on_1b", "outs_when_up", "inning", "inning_topbot", "hc_x", "hc_y", "tfs_deprecated", "tfs_zulu_deprecated", "pos2_person_id", "umpire", "sv_id", "vx0", "vy0", "vz0", "ax", "ay", "az", "sz_top", "sz_bot", "hit_distance_sc", "launch_speed", "launch_angle", "effective_speed", "release_spin_rate", "release_extension", "game_pk")
colnames(df) <- updated_names
new_cols <- c("plate_x", "plate_z", "pos1_person_id", "pos2_person_id.1", "pos3_person_id", "pos4_person_id", "pos5_person_id", "pos6_person_id", "p
@BillPetti
BillPetti / mlb_team_colors.R
Last active December 16, 2018 03:36
Hex color palette for all 30 Major League Teams
# Bill Petti
# September 2016
# hex color palette for all 30 Major League Teams
# colors courtesy of the awesome http://jim-nielsen.com/teamcolors/
mlb_team_colors <- c("Diamondbacks"="#A71930", "Braves"="#13274F", "Orioles"="#DF4601", "Red Sox"="#BD3039", "Cubs"="#CC3433", "White Sox"="#000000", "Reds"="#C6011F","Indians"="#E31937", "Rockies"="#333366","Tigers"="#0C2C56", "Astros"="#EB6E1F", "Royals"="#004687", "Angels"="#BA0021", "Dodgers"="#005A9C", "Marlins"="#0077C8", "Brewers"="#B6922E", "Twins"="#002B5C", "Mets"="#FF5910", "Yankees"="#003087", "Athletics"="#003831", "Phillies"="#E81828", "Pirates"="#FDB827", "Padres"="#7F411C", "Giants"="#8B6F4E", "Mariners"="#005C5C", "Cardinals"="#C41E3A", "Rays"="#8FBCE6", "Rangers"="#003278", "Blue Jays"="#134A8E", "Nationals"="#AB0003")
@BillPetti
BillPetti / daily_batter_loop.R
Last active May 24, 2016 02:17
daily_batter_loop.R
require(baseballr)
require(dplyr)
dates <- data.frame(date = seq(as.Date("2016-04-05"), as.Date("2016-04-10"), by = "day"))
test <- dates %>%
group_by(date) %>%
do(daily_batter_bref(.$date, .$date)) %>%
ungroup() %>%
select(date, everything()) %>%
arrange(Name)
@BillPetti
BillPetti / howmanyAltuves.r
Last active May 17, 2016 00:14
Function takes any distance in feet and converts it to the number of Jose Altuves
# Inspired by http://www.howmanyaltuves.com/
# function takes any distance in feet and converts it to the number of Jose Altuves
howmanyAltuves <- function(x) {
altuves <- x/5.416666666666667
return(paste0(round(altuves,1), " Altuves"))
}
@BillPetti
BillPetti / is_it_opening_day.R
Last active January 17, 2017 21:48
A function to check on how long until opening day
is_it_opening_day <- function(x) {
openingday <- as.Date("2017-04-03")
if ((openingday - Sys.Date()) > 1) {print(paste0("Nope. We've still got ", (openingday - Sys.Date()), " days to go. Stay strong."))} else
if ((openingday - Sys.Date()) == 1) {print(paste0("Nope, opening night is not the same thing as opening day. We've still got 1 day to go. But there are a few games on tonight."))}
else
if ((Sys.Date() - openingday) > 1) {print(paste0("Seriously? The season started ", (Sys.Date() - openingday), " days ago. Go watch some baseball!"))}
else
if ((Sys.Date() - openingday) == 1) {print(paste0("The season started yesterday. Go watch some baseball!"))}
else {print("Yes! Rejoice! It's Opening Day, All's right with the world.")}
}
@BillPetti
BillPetti / Cumulative.WAR.Animate.R
Created February 10, 2016 13:15
Code used for creating an animated plot of cumulative WAR for players through their age-23 season
require(dplyr)
require(ggplot2)
require(gganimate)
war <- read.csv("https://raw.githubusercontent.com/BillPetti/General-Code-Reference/master/FanGraphs_Leaderboard_Cum_WAR.csv", header=TRUE, na.strings="NA", dec=".", stringsAsFactors = FALSE)
war <- war %>% group_by(playerid) %>% arrange(Age) %>% mutate(Cumulative.Off = cumsum(Off), Cumulative.Def = cumsum(Def), Cumulative.WAR = cumsum(WAR))
plot <- ggplot(war, aes(Cumulative.Off, Cumulative.Def, frame = Age)) + geom_point(data = filter(war, Name == "Ted Williams"), colour = "red4", aes(size = Cumulative.WAR)) + geom_text(data = filter(war, Name == "Ted Williams"), aes(label = paste0("Williams:\n", Cumulative.WAR)), fontface = 2, size = 4.5, hjust=1, vjust=-1) + geom_point(data = filter(war, Name == "Ken Griffey Jr."), colour = "cyan3", aes(size = Cumulative.WAR)) + geom_text(data = filter(war, Name == "Ken Griffey Jr."), aes(label = paste0("Griffey, Jr.:\n", Cumulative.WAR)), fontface = 2, size = 4.5, hjust=0.5, vjust=-1) + geom_point(data = filter(war, N
@BillPetti
BillPetti / convert.magic.R
Created September 7, 2015 18:28
Function to convert multiple variables to different formats at once
convert.magic <- function(obj, type){
FUN1 <- switch(type, character = as.character, numeric = as.numeric, factor = as.factor)
out <- lapply(obj, FUN1)
as.data.frame(out)
}
@BillPetti
BillPetti / variable_list.R
Last active May 27, 2017 13:48
A quick function for creating a convenient variable reference table in R
# Quick script to make a more readable list of variables and attributes for dataframes in RStudio.
# Gives variable number, name, first observation, and the variable's class and returns a dataframe
# Modeled on SPSS's Variable View
variable_list <- function(x) {
data <- x[1,]
data <- t(data)
vars <- row.names(data)
num <- 1:length(vars)
data <- as.data.frame(cbind(num,vars,data))