R/MLB_Season_Stat.R

Defines functions MLB_Season_Stat

Documented in MLB_Season_Stat

#' Metric Calculation for MLB Season Batting and Pitching Data
#'
#' @param year MLB Season Year
#' @param statistic Batting or Pitching Statistic of Interest
#' @param BorP Batting or Pitching Data
#'
#' @return
#' @export
#'
#' @examples
#' year <- 1999
#' statistic <- "ERA"
#' BorP <- "P"
#' MLB_Season_Stat(year, statistic, BorP)
MLB_Season_Stat <- function(year, statistic, BorP){

  data <- MLB_Season(year, BorP)


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

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

  final <- c(data[where.max,1],max.data)
  final2 <- sprintf(paste("The team with the highest", statistic, "in", year, "was the", data[where.max,1], "with a", statistic, "of", max.data))


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