Skip to content

Commit

Permalink
Merge pull request #421 from nflverse/fix-kick-distance
Browse files Browse the repository at this point in the history
Fill kick_distance on kickoffs and punts resulting in touchbacks
  • Loading branch information
mrcaseb authored Aug 30, 2023
2 parents 597c668 + 882a7b2 commit d5d9d68
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: nflfastR
Title: Functions to Efficiently Access NFL Play by Play Data
Version: 4.5.1.9006
Version: 4.5.1.9007
Authors@R:
c(person(given = "Sebastian",
family = "Carl",
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- The function `calculate_series_conversion_rates()` no longer returns `NA` values if a small subset of pbp data is missing series on offense or defense. (#417)
- `fixed_drive` now correctly increments on plays where posteam lost a fumble but remains posteam because defteam also lost a fumble during the same play. (#419)
- nflfastR now fixes missing drive number counts in raw pbp data in order to provide accurate drive information. (#420)
- nflfastR now returns `kick_distance` on punts and kickoffs resulting in touchbacks. (#421)


# nflfastR 4.5.1
Expand Down
16 changes: 15 additions & 1 deletion R/helper_add_nflscrapr_mutations.R
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,21 @@ add_nflscrapr_mutations <- function(pbp) {
!is.na(.data$qtr)
) %>%
dplyr::ungroup() %>%
dplyr::mutate(game_id = as.character(.data$game_id)) %>%
dplyr::mutate(
game_id = as.character(.data$game_id),
# kick distance is NA on kickoffs and punts that result in touchbacks
# (unless the kick/punt) was caught between endzones
# we use yardline_100 to add it in those cases
is_relevant_touchback = as.numeric(is.na(.data$kick_distance) & .data$touchback == 1 & .data$play_type %in% c("punt", "kickoff")),
kick_distance = dplyr::case_when(
.data$is_relevant_touchback == 1 & .data$kickoff_attempt == 0 ~ yardline_100,
# gotta reverse yardline_100 on kickoffs
.data$is_relevant_touchback == 1 & .data$kickoff_attempt == 1 ~ 100 - yardline_100,
TRUE ~ .data$kick_distance
),
# drop helper variable
is_relevant_touchback = NULL
) %>%
fix_scrambles() %>%
make_model_mutations()

Expand Down
Binary file modified tests/testthat/expected_pbp.rds
Binary file not shown.

0 comments on commit d5d9d68

Please sign in to comment.