Skip to content

Instantly share code, notes, and snippets.

@bayesball
Created December 27, 2020 19:33
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 bayesball/a66625cd6a417fa5f4f26db8e3676b7c to your computer and use it in GitHub Desktop.
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.
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