Created
December 27, 2020 19:33
-
-
Save bayesball/a66625cd6a417fa5f4f26db8e3676b7c to your computer and use it in GitHub Desktop.
R function that provides new variables indicating if plate appearance goes through each possible count.
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
setup_work <- function(d2015){ | |
# d2015 is a Retrosheet play by play dataset | |
# removes all non-pitches from PITCH_SEQ_TX | |
d2015$pseq <- gsub("[.>123N+*]", "", d2015$PITCH_SEQ_TX) | |
# create a b and s sequence | |
d2015$pseq <- gsub("[BIPV]", "b", d2015$pseq) | |
d2015$pseq <- gsub("[CFKLMOQRST]", "s", d2015$pseq) | |
one.string3 <- function(ex, count){ | |
# create a vector of individual outcomes | |
ex.v <- unlist(strsplit(ex,"")) | |
# remove last X from vector | |
ex.v <- ex.v[-length(ex.v)] | |
# compute cumulative total of balls and strikes | |
n.balls <- cumsum(ex.v == "b") | |
n.strikes <- pmin(cumsum(ex.v == "s"), 2) | |
the_count <- paste(n.balls, n.strikes, sep="-") | |
count %in% the_count | |
} | |
# applies this function to all strings | |
d2015$c01 <- lapply(d2015$pseq, one.string3, "0-1") | |
d2015$c10 <- lapply(d2015$pseq, one.string3, "1-0") | |
d2015$c02 <- lapply(d2015$pseq, one.string3, "0-2") | |
d2015$c11 <- lapply(d2015$pseq, one.string3, "1-1") | |
d2015$c20 <- lapply(d2015$pseq, one.string3, "2-0") | |
d2015$c12 <- lapply(d2015$pseq, one.string3, "1-2") | |
d2015$c21 <- lapply(d2015$pseq, one.string3, "2-1") | |
d2015$c30 <- lapply(d2015$pseq, one.string3, "3-0") | |
d2015$c22 <- lapply(d2015$pseq, one.string3, "2-2") | |
d2015$c31 <- lapply(d2015$pseq, one.string3, "3-1") | |
d2015$c32 <- lapply(d2015$pseq, one.string3, "3-2") | |
d2015 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment