R/MLB_PostSeason_Stat.R

Defines functions MLB_PostSeason_Stat

Documented in MLB_PostSeason_Stat

#' Metric Average Calculation for MLB Post Season Batting Data
#'
#' @param year MLB Season Year of Interest
#' @param series MLS Post-Season Series of Interest
#' @param statistic Batting Statistic of Interest
#'
#' @return
#' @export
#'
#' @examples
#' year <- 1987
#' series <- 'ALCS'
#' statistic <- 'AB'
#' MLB_PostSeason_Stat(year, series, statistic)
MLB_PostSeason_Stat <- function(year, series, statistic){

  dataW <- MLB_PostSeason_Batting(year, series, 'W')
  dataL <- MLB_PostSeason_Batting(year, series, 'L')
  stat <- as.character(statistic)

  #Winner
  stat.dataW <- as.data.frame(sapply(dataW[,c(statistic)], as.numeric))
  stat.dataW1 <- na.omit(stat.dataW)
  colnames(stat.dataW1) <- c(stat)

  avg.dataW <- mean(stat.dataW1[,1])

  #Loser
  stat.dataL <- as.data.frame(sapply(dataL[,c(statistic)], as.numeric))
  stat.dataL1 <- na.omit(stat.dataL)
  colnames(stat.dataW1) <- c(stat)

  avg.dataL <- mean(stat.dataL1[,1])

  finalW <- sprintf(paste("The winner of the", series, "in", year, "had an average", statistic, "of", avg.dataW))
  finalL <- sprintf(paste("The loser of the", series, "in", year, "had an average", statistic, "of", avg.dataL))

  final <- c(finalW, finalL)

  return(final)
}
madeline-peyton/B581Final documentation built on Dec. 23, 2021, 11:16 p.m.