R/league_shot_zone_summary.R

#' @title Zone Averages - League
#' @description gets and summarizes data for every shot taken in league for each season since shot details were made available
#' @keywords NBA stat.nba.com
#' @importFrom magrittr %>%
#' @export
#' @examples
#' league_shot_zone_summary()

league_shot_zone_summary <- function(save=T) {

  years <- c('1996-97','1997-98','1998-99','1998-99','1998-99','1997-98',
             '1998-99','1999-00','2000-01','2001-02','2001-02','2001-02',
             '2001-02','2002-03','2003-04','2004-05','2005-06','2006-07',
             '2007-08','2008-09','2009-10','2010-11','2011-12','2012-13',
             '2013-14','2014-15','2015-16','2016-17','2017-18','2018-19')


  league_zone_df <- data.frame(matrix(ncol = 6, nrow = 0))
  colnames(league_zone_df) <- c("shot_zone_range","shot_zone_area","shot_zone_basic","shots_attempted",
                                "league_pct","year")
  for (i in 1:length(years)) {
    yr_df <- get_player_shot_chart_data(player='all', season=years[i])
    yr_summary <- yr_df %>%
      dplyr::group_by(shot_zone_range, shot_zone_area, shot_zone_basic) %>%
      dplyr::summarise(league_shots_made = sum(shot_made_flag),
                       league_shots_attempted = sum(shot_attempted_flag),
                       league_pct = sum(shot_made_flag) / sum(shot_attempted_flag))
    yr_summary$year <- years[i]
    league_zone_df <- league_zone_df %>%
      dplyr::bind_rows(yr_summary)
    print(years[i])
  }

  if (save==T) {
    utils::write.csv(league_zone_df, file = 'data-raw/league-shot-chart-summary.csv', row.names = F)
  }

  return(league_zone_df)

}
emilykuehler/basketballstatsR documentation built on May 31, 2019, 10:01 a.m.