R/MLB_Team_Stat_Max.R

Defines functions MLB_Team_Stat_Max

Documented in MLB_Team_Stat_Max

#' Metric Maximum Calculation for MLB Team Batting and Pitching Data
#'
#' @param TeamAbbr MLB Team Abbreviation
#' @param year MLB Season Year
#' @param statistic Statistic of Interest
#' @param BorP Batting or Pitching Data
#'
#' @return
#' @export
#'
#' @examples
#' TeamAbbr <- 'HOU'
#' year <- 2008
#' statistic <- 'SO'
#' BorP <- 'B'
#' MLB_Team_Stat_Max(TeamAbbr, year, statistic, BorP)
MLB_Team_Stat_Max <- function(TeamAbbr, year, statistic, BorP){

  data <- MLB_Team(TeamAbbr, year, BorP)

  stat.data <- as.data.frame(sapply(data[,c(statistic)], as.numeric))
  stat.data1 <- na.omit(stat.data)
  stat <- as.character(statistic)
  colnames(stat.data1) <- c(stat)

  max.data <- max(stat.data1)
  where.max <- which.max(stat.data1[,stat])

  if (BorP == 'B'){
    final <- c(data[where.max,3], data[where.max,2], max.data)
    final2 <- sprintf(paste("The player with the highest", statistic, "for", TeamAbbr, "in", year, "was", data[where.max,3], data[where.max,2], "with a", statistic, "of", max.data))
  }
  if (BorP == 'P'){
    final <- c(data[where.max,3], "P", max.data)
    final2 <- sprintf(paste("The player with the highest", statistic, "for", TeamAbbr, "in", year, "was", data[where.max,3], "P", "with a", statistic, "of", max.data))
  }

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