This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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")) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.")} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)) |
NewerOlder