Skip to content

Commit

Permalink
parse old pbp
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcaseb committed Mar 1, 2024
1 parent 72f8961 commit 623b4c6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 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.6.1.9004
Version: 4.6.1.9005
Authors@R:
c(person(given = "Sebastian",
family = "Carl",
Expand Down
45 changes: 39 additions & 6 deletions R/build_playstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,53 @@ build_playstats <- function(seasons = nflreadr::most_recent_season(),
Will go on sequentially...", wrap = TRUE)
}

games <- nflreadr::load_schedules() %>%
dplyr::filter(!is.na(.data$result), .data$season %in% seasons) %>%
games <- nflreadr::load_schedules(seasons = seasons) %>%
dplyr::filter(!is.na(.data$result)) %>%
dplyr::pull(.data$game_id)

p <- progressr::progressor(along = games)

l <- furrr::future_map(
games,
function(id, p, dir, skip_local){
function(id, p = NULL, dir, skip_local){
season <- substr(id, 1, 4)
raw_data <- load_raw_game(id, dir = dir, skip_local = skip_local)
out <- raw_data$data$viewer$gameDetail$plays[, c("playId", "playStats")]
if (season <= 2001){
drives <- raw_data[[1]][["drives"]] %>%
purrr::keep(is.list)
out <- tibble::tibble(d = drives) %>%
tidyr::unnest_wider(d) %>%
tidyr::unnest_longer(plays) %>%
tidyr::unnest_wider(plays, names_sep = "_") %>%
dplyr::select(playId = plays_id, playStats = plays_players) %>%
tidyr::unnest_longer(playStats) %>%
tidyr::unnest_longer(playStats) %>%
tidyr::unnest_wider(playStats) %>%
dplyr::mutate(
playId = as.integer(playId),
statId = as.integer(statId),
yards = as.integer(yards),
team.id = NA_character_
) %>%
dplyr::select(-"sequence") %>%
dplyr::rename(
team.abbreviation = "clubcode",
gsis.Player.id = "playStats_id"
) %>%
tidyr::nest(
playStats = c(
statId,
yards,
playerName,
team.id,
team.abbreviation,
gsis.Player.id
)
)
} else {
out <- raw_data$data$viewer$gameDetail$plays[, c("playId", "playStats")]
}
out$game_id <- as.character(id)
# out$desc <- raw_data$data$viewer$gameDetail$plays$playDescriptionWithJerseyNumbers
p(sprintf("ID=%s", as.character(id)))
out
},
Expand Down Expand Up @@ -50,7 +84,6 @@ build_playstats <- function(seasons = nflreadr::most_recent_season(),
"team_abbr" = "team_abbreviation",
"player_name",
"gsis_player_id",
# "desc"
) %>%
dplyr::mutate_if(
.predicate = is.character,
Expand Down

0 comments on commit 623b4c6

Please sign in to comment.