Skip to content

Instantly share code, notes, and snippets.

@gghatano
Created March 6, 2014 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gghatano/9396786 to your computer and use it in GitHub Desktop.
Save gghatano/9396786 to your computer and use it in GitHub Desktop.
calculate the spin_rate distribution under condition : HIT or STRIKE
library(dplyr)
library(magrittr)
library(data.table)
library(ggplot2)
# pitch f/x data 2013
dat = fread("2013.csv")
dat = dat %>% filter(sv_id !="NA")
# hit event
hits = c("In play, run(s)", "In play, no out")
strikes = c("Called Strike", "Swinging Strike")
## data processing
# hit flag
dat = dat %>% mutate(h_fl = ifelse(des %in% hits, "HIT", "NOT")) %>% mutate(h_fl = as.factor(h_fl))
# strike flag
dat = dat %>% mutate(strike_fl = ifelse(des %in% strikes, "STRIKE", "NOT")) %>% mutate(strike_fl = as.factor(strike_fl))
# hit or strike
dat = dat %>% filter(h_fl == "HIT" | strike_fl =="STRIKE")
dat = dat %>% mutate(hit_or_strike = ifelse(h_fl=="HIT", "HIT", "STRIKE"))
# visualize
pingr(
dat %>% filter(sv_id!="NA") %>% select(spin_rate, hit_or_strike) %>%
mutate(spin_rate = as.numeric(spin_rate)) %>%
ggplot() + geom_histogram(aes(x=spin_rate, col="red"), stat="density", binwidth=100) + facet_grid(hit_or_strike ~ .) +
ggtitle("spin_rate distribution (Hit or not)") +
theme(legend.position="NULL") + theme(plot.title=element_text(face="bold", size=20))
)
ping(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment