R/get_bb_shot_class.R

#' Get basketball shot classes from a game's dataframe.
#'
#' \code{get_bb_shot_class} is used to get the shot class ("Corner3", "Reg3", etc.) of shots in a given dataframe. The function
#' returns a vector of shot types.
#' @keywords get_bb_shot_class
#' @export
#' @param data a dataframe object
#' @param x_col column of x coordinates
#' @param y_col column of y coordinates

get_bb_shot_class <- function(data, x, y){

    # get needed distance calculation
    #--------------------------------
    data$xt <- ifelse(data$x < 470,
                      data$x,
                      940 - data$x)
    data$yt <- ifelse(data$x < 470,
                      data$y,
                      500 - data$y)
    data$dist2 <- sqrt(((data$xt-50)^2) + ((data$yt-250)^2))

    # get shot type into new column
    # -----------------------------
    data$shotClass <- ifelse(data$xt < 140 & (data$yt > 470 | data$yt < 30),
                             "Corner3", ifelse(data$dist2 < 60, "Rim",
                                               ifelse(data$dist2 > 240 & data$xt > 140,
                                                      "Reg3", ifelse((data$yt > 170 & data$yt < 330) & data$xt < 190, "Paint", "Mid" ))))
    # make sure that regular shots are not
    # confused with foul shots.
    # ---------------------------------
    data$shotClass <- ifelse(data$type != "Foul", data$shotClass, "FoulShot")

    # return vector of shot classes
    # -----------------------------
    data[['shotClass']]
}
mistermichaelll/statcrewRC documentation built on June 1, 2019, 3:57 a.m.