diff --git a/DESCRIPTION b/DESCRIPTION index 44c1c597..f042b3be 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: nflfastR Title: Functions to Efficiently Access NFL Play by Play Data -Version: 5.0.0.9001 +Version: 5.0.0.9002 Authors@R: c(person(given = "Sebastian", family = "Carl", diff --git a/NEWS.md b/NEWS.md index 3c885247..df7ffeb9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,7 @@ - Fixed a bug where `calculate_stats()` incorrectly counted `receiving_air_yards`. (#500) - Fixed a bug where `vegas_wp` variables were broken when `spread_line` data was missing. (#503) +- Fixed a bug where `calculate_stats()` incorrectly calculated `target_share` and `air_yards_share` when `summary_level = "season"`. (#505) # nflfastR 5.0.0 diff --git a/R/calculate_stats.R b/R/calculate_stats.R index 17657dd1..a4748423 100644 --- a/R/calculate_stats.R +++ b/R/calculate_stats.R @@ -122,9 +122,13 @@ calculate_stats <- function(seasons = nflreadr::most_recent_season(), team_stats = paste0(paste(stat_id, collapse = ";"), ";"), team_play_air_yards = sum((stat_id %in% 111:112) * yards) ) %>% - dplyr::group_by(.data$season, .data$week, .data$team) %>% + # compute team targets and team air yards for calculation of target share + # and air yard share. Since it's relative, we need to be careful with the groups + # depending on summary level + dplyr::group_by(!!!rlang::data_syms( + if (summary_level == "season") c("season", "team") else c("season", "week", "team") + )) %>% dplyr::mutate( - # for calculation of target share and air yard share team_targets = sum(stat_id == 115), team_air_yards = sum((stat_id %in% 111:112) * yards) ) %>%