R/MLB_Player_Stat_Max.R

Defines functions MLB_Player_Stat_Max

Documented in MLB_Player_Stat_Max

#' Metric Maximum Calculation for MLB Player Batting/Pitching Data
#'
#' @param name Player Name
#' @param statistic Batting/Pitching Statistic of Interest
#'
#' @return
#' @export
#'
#' @examples
#' name <- "Johnny Bench"
#' statistic <- "HR"
#' MLB_Player_Stat_Max(name, statistic)
MLB_Player_Stat_Max <- function(name, statistic){

  data <- MLB_Player(name)

  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])

  final2 <- sprintf(paste(name, "had the highest", statistic, "of", max.data, "in", data[where.max,1], "with", data[where.max,3]))

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