From 574dd6769c6ee382bbcc68f0e6114348205ad4a2 Mon Sep 17 00:00:00 2001 From: Dennis Brookner Date: Sun, 6 Feb 2022 17:44:23 -0500 Subject: [PATCH 01/17] Update _pkgdown.yml --- _pkgdown.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/_pkgdown.yml b/_pkgdown.yml index 9d18c86..940089f 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -6,3 +6,23 @@ navbar: data: text: Data from past seasons href: https://github.com/Puntalytics/puntr-data/tree/master/data +reference: +- title: Get data + desc: Import punting play-by-play data and calculate [Puntalytics metrics](https://puntalytics.github.io/metrics.html) +- contents: + - import_punts + - trust_the_process + - punt_trim + - calculate_all +- subtitle: College data +- contents: + - import_college_punts + - college_to_pro +- title: Summarize data + desc: Turn a play-by-play dataframe into a dataframe summarizing player stats +- contents: + - starts_with("by_punter") +- title: Deprecated +- contents: + - starts_with("create") + - import_seasons From 5604ad8a521533d9c165300426c8859c5299a731 Mon Sep 17 00:00:00 2001 From: Dennis Brookner Date: Sun, 6 Feb 2022 17:52:59 -0500 Subject: [PATCH 02/17] Update puntr.Rmd --- vignettes/puntr.Rmd | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/vignettes/puntr.Rmd b/vignettes/puntr.Rmd index 9a87a1f..20cd4d8 100644 --- a/vignettes/puntr.Rmd +++ b/vignettes/puntr.Rmd @@ -23,8 +23,9 @@ library(tidyverse) # always a good idea to do this too For speed, we've already scraped (using [`nflfastR`](https://www.nflfastr.com/)) and saved punting data for the 1999-2020 seasons. The easiest thing to do is download the `puntr-data` repo [here](https://github.com/Puntalytics/puntr-data/tree/master/data), and then point `puntr::import_punts()` to your local copy of the data. You can also download the data directly each time; this takes around 15 minutes. Import, clean, and calculate as follows: ```{r imports, message=FALSE} -#punts_raw <- import_punts(1999:2020, local=TRUE, path=your_local_path) # recommended -punts_raw <- import_punts(2018:2020) # This takes ~15 minutes +# punts_raw <- import_punts(1999:2020, local=TRUE, path=your_local_path) # recommended +# punts_raw <- import_punts(1999:2020) # This takes ~15 minutes +punts_raw <- import_punts(2018:2020) punts_cleaned <- trust_the_process(punts_raw) # clean punts <- calculate_all(punts_cleaned) # calculate custom Puntalytics metrics ``` @@ -83,6 +84,7 @@ Let's take a look at some of the columns in this data frame: punters %>% arrange(desc(pEPA)) %>% select(punter_player_name, Gross, Net, pEPA) %>% + mutate(across(where(is.numeric), round, 3)) %>% rmarkdown::paged_table() ``` @@ -95,6 +97,7 @@ which gives every unique punter season a row. punter_seasons %>% arrange(desc(pEPA)) %>% select(punter_player_name, season, Gross, Net, pEPA) %>% + mutate(across(where(is.numeric), round, 3)) %>% rmarkdown::paged_table() ``` And finally, to compare punter **games**, use @@ -109,7 +112,7 @@ These dataframes - `punts`, `punters`, `punter_seasons` and `punter_games` - sho ## Using `puntr` with college data -***NOTE: `puntr` was successfully migrated from `cfbscrapR` to `cfbfastR` in version 1.2.2*** +***NOTE: `puntr` was successfully migrated from `cfbscrapR` to `cfbfastR` in version 1.2.2*** ***NOTE: The `by_` family of summary functions have not yet been tested for `cfbfastR` data, but might work.*** `puntr` can also handle punting data for college football, piggybacking off of the scraping abilities of the [`cfbfastR`](https://saiemgilani.github.io/cfbfastR/) package. You need at least 3 seasons worth of data to run `calculate_all()`. Import and clean as follows: From d782ddf437e860f6f1500b7f270e797c3689fd4d Mon Sep 17 00:00:00 2001 From: Dennis Brookner Date: Sun, 6 Feb 2022 18:14:43 -0500 Subject: [PATCH 03/17] Update pkgdown.yaml --- .github/workflows/pkgdown.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index 63cbb18..8c86d65 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -1,5 +1,5 @@ # Workflow derived from https://github.com/r-lib/actions/tree/master/examples -# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help test on: push: branches: [main, master] From ff5be6a5eb8e7f00baea3d3183f16feb34c72dcb Mon Sep 17 00:00:00 2001 From: Dennis Brookner Date: Sun, 6 Feb 2022 18:18:30 -0500 Subject: [PATCH 04/17] Delete pkgdown.yaml Workflows don't get run from this branch; deleting the file to avoid confusion --- .github/workflows/pkgdown.yaml | 35 ---------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 .github/workflows/pkgdown.yaml diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml deleted file mode 100644 index 8c86d65..0000000 --- a/.github/workflows/pkgdown.yaml +++ /dev/null @@ -1,35 +0,0 @@ -# Workflow derived from https://github.com/r-lib/actions/tree/master/examples -# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help test -on: - push: - branches: [main, master] - release: - types: [published] - workflow_dispatch: - -name: pkgdown - -jobs: - pkgdown: - runs-on: ubuntu-latest - env: - GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} - steps: - - uses: actions/checkout@v2 - - - uses: r-lib/actions/setup-pandoc@v1 - - - uses: r-lib/actions/setup-r@v1 - with: - use-public-rspm: true - - - uses: r-lib/actions/setup-r-dependencies@v1 - with: - extra-packages: pkgdown - needs: website - - - name: Deploy package - run: | - git config --local user.name "$GITHUB_ACTOR" - git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" - Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE)' From fc6dd20f671155bee049778cd294b0061d212197 Mon Sep 17 00:00:00 2001 From: Dennis Brookner Date: Sun, 6 Feb 2022 18:18:57 -0500 Subject: [PATCH 05/17] Delete pkgdown_test.yaml Workflows are not run from this branch; deleting this file to avoid confusion --- .github/workflows/pkgdown_test.yaml | 45 ----------------------------- 1 file changed, 45 deletions(-) delete mode 100644 .github/workflows/pkgdown_test.yaml diff --git a/.github/workflows/pkgdown_test.yaml b/.github/workflows/pkgdown_test.yaml deleted file mode 100644 index 551bb17..0000000 --- a/.github/workflows/pkgdown_test.yaml +++ /dev/null @@ -1,45 +0,0 @@ -on: pull_request - -name: Test build of pkgdown - -jobs: - pkgdown: - runs-on: macOS-latest - env: - GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} - steps: - - uses: actions/checkout@v2 - - - uses: r-lib/actions/setup-r@master - - - uses: r-lib/actions/setup-pandoc@master - - - name: Query dependencies - run: | - install.packages('remotes') - saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2) - writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") - shell: Rscript {0} - - - name: Cache R packages - uses: actions/cache@v1 - with: - path: ${{ env.R_LIBS_USER }} - key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} - restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- - - - name: Install dependencies - run: | - remotes::install_deps(dependencies = TRUE) - install.packages(c("pkgdown", "roxygen2")) - shell: Rscript {0} - - - name: Install package - run: R CMD INSTALL . - - - name: Deploy package - run: | - git config --local user.email "actions@github.com" - git config --local user.name "GitHub Actions" - Rscript -e 'roxygen2::roxygenise()' - Rscript -e 'pkgdown::deploy_to_branch(new_process = TRUE, branch = "gh-pages-test")' From 0ea59ea9334ffd920645b2e7a7375f126acc0bc0 Mon Sep 17 00:00:00 2001 From: Dennis Brookner Date: Sun, 6 Feb 2022 18:23:15 -0500 Subject: [PATCH 06/17] Update .gitignore --- .github/.gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/.gitignore b/.github/.gitignore index 2d19fc7..0ad3b70 100644 --- a/.github/.gitignore +++ b/.github/.gitignore @@ -1 +1,2 @@ *.html +.github/workflows/pkgdown.yaml From 2da4153b71b24065f1a0477aca24d0da64706d0a Mon Sep 17 00:00:00 2001 From: Dennis Brookner Date: Sun, 6 Feb 2022 18:25:08 -0500 Subject: [PATCH 07/17] Update .gitignore --- .github/.gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/.gitignore b/.github/.gitignore index 0ad3b70..730879b 100644 --- a/.github/.gitignore +++ b/.github/.gitignore @@ -1,2 +1,3 @@ +.github/.gitignore *.html .github/workflows/pkgdown.yaml From 4d5840b4c51288dc6d503b29c049534f5abb6ea3 Mon Sep 17 00:00:00 2001 From: dennisbrookner Date: Sun, 6 Feb 2022 18:27:38 -0500 Subject: [PATCH 08/17] using gitignore to hopefully resolve conflict --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 7c06555..bcdfd8f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ .Ruserdata .DS_Store docs + +.gitignore +.github/workflows/* \ No newline at end of file From 344d293d3436f9ce2d2166ee6e77be331449cc84 Mon Sep 17 00:00:00 2001 From: dennisbrookner Date: Sun, 6 Feb 2022 18:40:28 -0500 Subject: [PATCH 09/17] commiting empty files for now --- .github/.gitignore | 3 +-- .github/workflows/pkgdown.yaml | 0 .github/workflows/pkgdown_test.yaml | 0 .gitignore | 2 -- 4 files changed, 1 insertion(+), 4 deletions(-) create mode 100644 .github/workflows/pkgdown.yaml create mode 100644 .github/workflows/pkgdown_test.yaml diff --git a/.github/.gitignore b/.github/.gitignore index 730879b..f920f88 100644 --- a/.github/.gitignore +++ b/.github/.gitignore @@ -1,3 +1,2 @@ -.github/.gitignore + *.html -.github/workflows/pkgdown.yaml diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml new file mode 100644 index 0000000..e69de29 diff --git a/.github/workflows/pkgdown_test.yaml b/.github/workflows/pkgdown_test.yaml new file mode 100644 index 0000000..e69de29 diff --git a/.gitignore b/.gitignore index bcdfd8f..510f32f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,3 @@ .DS_Store docs -.gitignore -.github/workflows/* \ No newline at end of file From 26af31873d53d974b8060eb666821d95981a8d92 Mon Sep 17 00:00:00 2001 From: dennisbrookner Date: Sun, 6 Feb 2022 19:06:59 -0500 Subject: [PATCH 10/17] contents of test workflow --- .github/workflows/pkgdown_test.yaml | 65 +++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/.github/workflows/pkgdown_test.yaml b/.github/workflows/pkgdown_test.yaml index e69de29..d0feb2e 100644 --- a/.github/workflows/pkgdown_test.yaml +++ b/.github/workflows/pkgdown_test.yaml @@ -0,0 +1,65 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/master/examples and edited by me (Dennis) +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help + +on: + pull_request: + workflow_dispatch: + +name: Test build of pkgdown + +jobs: + pkgdown: + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v2 + + - uses: r-lib/actions/setup-pandoc@v1 + + - uses: r-lib/actions/setup-r@v1 + with: + use-public-rspm: true + + - name: Query dependencies + run: | + install.packages('remotes') + saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2) + writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") + shell: Rscript {0} + + - name: Cache R packages + uses: actions/cache@v1 + with: + path: ${{ env.R_LIBS_USER }} + key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} + restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- + + - name: Install dependencies + run: | + remotes::install_deps(dependencies = TRUE) + install.packages(c("pkgdown", "roxygen2")) + shell: Rscript {0} + + - name: Install package + run: R CMD INSTALL . + + - name: Deploy package + run: | + git config --local user.email "actions@github.com" + git config --local user.name "GitHub Actions" + Rscript -e 'roxygen2::roxygenise()' + Rscript -e 'pkgdown::deploy_to_branch(new_process = TRUE, branch = "gh-pages-test")' + +# - uses: r-lib/actions/setup-r-dependencies@v1 +# with: +# extra-packages: pkgdown +# needs: website + +# - name: Deploy package +# run: | +# git config --local user.name "$GITHUB_ACTOR" +# git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" +# Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE, branch = "gh-pages-test")' + + From bea2c72f46cd1c6ea374a9c8b07dad08a5234148 Mon Sep 17 00:00:00 2001 From: dennisbrookner Date: Sun, 6 Feb 2022 19:10:09 -0500 Subject: [PATCH 11/17] adding workflow contents --- .github/workflows/pkgdown.yaml | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index e69de29..98b0908 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -0,0 +1,37 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/master/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + push: + branches: [main, master] + release: + types: [published] + workflow_dispatch: + +name: pkgdown + +jobs: + pkgdown: + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v2 + + - uses: r-lib/actions/setup-pandoc@v1 + + - uses: r-lib/actions/setup-r@v1 + with: + use-public-rspm: true + + - uses: r-lib/actions/setup-r-dependencies@v1 + with: + extra-packages: pkgdown, roxygen2 + needs: website + + - name: Deploy package + run: | + git config --local user.name "$GITHUB_ACTOR" + git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" + Rscript -e 'roxygen2::roxygenize()' + Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE)' + From 96661d2a3ff36d139e66fe4a31ecae8cd8742024 Mon Sep 17 00:00:00 2001 From: Dennis Brookner Date: Sun, 6 Feb 2022 21:19:46 -0500 Subject: [PATCH 12/17] Update DESCRIPTION --- DESCRIPTION | 3 --- 1 file changed, 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ecd3489..be14206 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,9 +16,6 @@ Imports: cfbfastR, dplyr, forcats, - ggimage, - ggplot2, - ggrepel, glue, magrittr, nflfastR, From 018cccd1ac5ed80ab455f37a242103e2ca84e96c Mon Sep 17 00:00:00 2001 From: Dennis Brookner Date: Mon, 7 Feb 2022 11:56:58 -0500 Subject: [PATCH 13/17] Update pkgdown_test.yaml --- .github/workflows/pkgdown_test.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/pkgdown_test.yaml b/.github/workflows/pkgdown_test.yaml index 5687328..bc3ff53 100644 --- a/.github/workflows/pkgdown_test.yaml +++ b/.github/workflows/pkgdown_test.yaml @@ -31,4 +31,5 @@ jobs: git config --local user.email "actions@github.com" git config --local user.name "GitHub Actions" Rscript -e 'roxygen2::roxygenise()' + Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE, branch = "gh-pages-test")' From b3fc47ab37537ef0ee4abb11476e6c849c96fdcd Mon Sep 17 00:00:00 2001 From: Dennis Brookner Date: Mon, 7 Feb 2022 12:31:52 -0500 Subject: [PATCH 14/17] Update import.R --- R/import.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/import.R b/R/import.R index ae17b8f..85968a3 100644 --- a/R/import.R +++ b/R/import.R @@ -34,9 +34,9 @@ import_punts <- function(years, local = FALSE, path = NULL) { } #' Import play-by-play data -#' @description Grab all play-by-play data, not just punts. This function pulls data directly from the \code{nflfastR-data} repo, and -#' is purely a wrapper for ease of use. -#' @param years A year or range of years between 1999 and 2020, inclusive +#' @description DEPRECATED: Grab all play-by-play data, not just punts, from the \code{nflfastR-data} repo. This will be much slower than +#' using the \code{nflfastR::load_pbp()} or \code{nflfastR::update_pbp()} functions. +#' @param years A year or range of years between 1999 and 2021, inclusive #' @return A tibble \code{pbp} containing play-by-play data for the specified years #' @examples #' \dontrun{ From d3d6c9c0502662717d28faabfed8a66217b65d61 Mon Sep 17 00:00:00 2001 From: Dennis Brookner Date: Mon, 7 Feb 2022 12:45:09 -0500 Subject: [PATCH 15/17] Update mini2.R --- R/mini2.R | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/R/mini2.R b/R/mini2.R index be38142..ea9dcb0 100644 --- a/R/mini2.R +++ b/R/mini2.R @@ -99,15 +99,24 @@ custom_summary <- function(data, ...) { SHARP_RERUN_OF = mean(SHARP_RERUN_OF, na.rm = TRUE), SHARP_RERUN_PD = mean(SHARP_RERUN_PD, na.rm = TRUE), ..., - team = getmode_local(posteam), - team_logo_espn = getmode_local(team_logo_espn), - team_color = getmode_local(team_color), - team_color2 = getmode_local(team_color2), + team = puntr::getmode(posteam), + team_logo_espn = puntr::getmode(team_logo_espn), + team_color = puntr::getmode(team_color), + team_color2 = puntr::getmode(team_color2), ) return(.summary) } - -getmode_local <- function(v) { - uniqv <- unique(v) - uniqv[which.max(tabulate(match(v, uniqv)))] +#' Find the mode of a column +#' @description Hilariously, R does not have a built-in method for finding the mode of a column. \code{puntr} has included an internal +#' helper function for this purpose for a while, and now it can be yours too! +#' @param column The dataframe column (or any other list) from which you would like to extract the most frequently occuring item +#' @return The mode of \code{column} +#' @examples +#' \dontrun{ +#' players_most_common_team <- getmode(punts_by_punter$posteam) +#' } +#' @export +getmode <- function(column) { + uniqv <- unique(column) + uniqv[which.max(tabulate(match(column, uniqv)))] } From 8ba9c488af7ccc3d2e3514ae6fa61813e02ca31c Mon Sep 17 00:00:00 2001 From: Dennis Brookner Date: Mon, 7 Feb 2022 12:49:27 -0500 Subject: [PATCH 16/17] Update _pkgdown.yml --- _pkgdown.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/_pkgdown.yml b/_pkgdown.yml index 940089f..5eec0d1 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -22,6 +22,9 @@ reference: desc: Turn a play-by-play dataframe into a dataframe summarizing player stats - contents: - starts_with("by_punter") +- title: Miscellaneous + contents: + - getmode - title: Deprecated - contents: - starts_with("create") From c3f6cc4aba8fbc2bc976beeb9251857c4611cbca Mon Sep 17 00:00:00 2001 From: dennisbrookner Date: Fri, 16 Aug 2024 16:14:36 -0400 Subject: [PATCH 17/17] Drop CFB support; pump version to 1.4 --- DESCRIPTION | 3 +- R/college.R | 162 ++++++++++++++++++++++++++-------------------------- 2 files changed, 82 insertions(+), 83 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ecd3489..08c4b97 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: puntr Type: Package Title: Analysis of Punting License: MIT -Version: 1.3 +Version: 1.4 Authors@R: c( person("Dennis", "Brookner", role = c("aut", "cre"), email = "debrookner@gmail.com"), person("Raphael", "LadenGuindon", role = "aut")) @@ -13,7 +13,6 @@ URL: https://puntalytics.github.io/puntr, https://github.com/Puntalytics/puntr Encoding: UTF-8 LazyData: true Imports: - cfbfastR, dplyr, forcats, ggimage, diff --git a/R/college.R b/R/college.R index b4cd76d..4bc40c0 100644 --- a/R/college.R +++ b/R/college.R @@ -1,84 +1,84 @@ -#' Import college punting data -#' @description Import college punting data for seasons in the scope of \code{cfbfastR} (back to 2014). -#' This function is a wrapper around \code{cfbfastR::load_cfb_pbp}. -#' @param years A year or range of years to be scraped -#' @return A tibble \code{punts} of punts in the \code{cfbfastR} format -#' @examples -#' \dontrun{ -#' import_college_punts(2018:2021) +#' #' Import college punting data +#' #' @description Import college punting data for seasons in the scope of \code{cfbfastR} (back to 2014). +#' #' This function is a wrapper around \code{cfbfastR::load_cfb_pbp}. +#' #' @param years A year or range of years to be scraped +#' #' @return A tibble \code{punts} of punts in the \code{cfbfastR} format +#' #' @examples +#' #' \dontrun{ +#' #' import_college_punts(2018:2021) +#' #' } +#' #' @export +#' import_college_punts <- function(years) { +#' punts <- purrr::map_df(years, function(x){ +#' cfbfastR::load_cfb_pbp(x) %>% +#' dplyr::filter(punt == 1) %>% +#' dplyr::mutate(season = x) +#' }) +#' return(punts) #' } -#' @export -import_college_punts <- function(years) { - punts <- purrr::map_df(years, function(x){ - cfbfastR::load_cfb_pbp(x) %>% - dplyr::filter(punt == 1) %>% - dplyr::mutate(season = x) - }) - return(punts) -} - -#' Convert college data to \code{puntr} format #' -#' @description Rename columns and process data such that the output can be plugged directly into \code{puntr::calculate_all}, -#' and the output of that can be plugged directly into \code{puntr::create_mini} (or \code{puntr::create_miniY}). -#' @param punts A data frame containing punts in the cfbfastR format -#' @param power_five Logical, defaults to TRUE to include only punters from Power 5 teams -#' @return A tibble \code{punts} in a format usable for \code{puntr::calculate_all} -#' @examples -#' \dontrun{ -#' college_to_pro(punts) +#' #' Convert college data to \code{puntr} format +#' #' +#' #' @description Rename columns and process data such that the output can be plugged directly into \code{puntr::calculate_all}, +#' #' and the output of that can be plugged directly into \code{puntr::create_mini} (or \code{puntr::create_miniY}). +#' #' @param punts A data frame containing punts in the cfbfastR format +#' #' @param power_five Logical, defaults to TRUE to include only punters from Power 5 teams +#' #' @return A tibble \code{punts} in a format usable for \code{puntr::calculate_all} +#' #' @examples +#' #' \dontrun{ +#' #' college_to_pro(punts) +#' #' } +#' #' @export +#' college_to_pro <- function(punts, power_five = TRUE) { +#' +#' punts <- punts %>% +#' #dplyr::rename(season = year) %>% +#' dplyr::mutate(GrossYards = play_text %>% +#' stringr::str_extract("punt for [:digit:]+") %>% +#' stringr::str_extract("[:digit:]+") %>% +#' as.numeric()) %>% +#' dplyr::filter(!is.na(GrossYards)) %>% +#' dplyr::mutate(return_yards = as.numeric(yds_punt_return), +#' return_yards = ifelse(is.na(return_yards),0,return_yards), +#' return_yards = ifelse(stringr::str_detect(play_text,"loss"),-1*return_yards,return_yards)) %>% +#' #dplyr::filter(!is.na(return_yards)) %>% +#' dplyr::mutate(punter_player_name = play_text %>% +#' stringr::str_extract(".+(?= punt for)")) %>% +#' dplyr::mutate(YardsFromOwnEndZone = as.integer(100 - yards_to_goal)) %>% +#' dplyr::filter(YardsFromOwnEndZone <= 70) %>% +#' dplyr::mutate(touchback = play_text %>% stringr::str_detect("ouchback")) %>% +#' dplyr::mutate(return_yards = dplyr::if_else(touchback, 0, return_yards)) %>% +#' dplyr::mutate(NetYards = GrossYards - return_yards) %>% +#' dplyr::mutate(GrossYards = dplyr::if_else(touchback, as.numeric(GrossYards-20), as.numeric(GrossYards))) %>% +#' dplyr::mutate(punt_out_of_bounds = play_text %>% stringr::str_detect("out.of.bounds")) %>% +#' dplyr::mutate(punt_fair_catch = play_text %>% stringr::str_detect("air catch")) %>% +#' dplyr::mutate(punt_downed = play_text %>% stringr::str_detect("downed")) %>% +#' dplyr::mutate(PD = dplyr::if_else(YardsFromOwnEndZone >=41, 1, 0)) %>% +#' #rename columns to avoid breaking calculate_all() +#' dplyr::rename(ep_before_cfb = ep_before, +#' ep_after_cfb = ep_after) +#' +#' +#' +#' # Pull from data-repo to avoid requiring cfbd API key +#' #team_info <- cfbfastR::cfbd_team_info() +#' team_info <- readRDS(url("https://github.com/saiemgilani/cfbfastR-data/blob/master/team_info/rds/cfb_team_info_2020.rds?raw=true")) +#' +#' if(power_five) { +#' team_info <- team_info %>% +#' dplyr::filter(conference %in% c("Pac-12","SEC","Big Ten","Big 12","ACC")) +#' } +#' +#' team_info <- team_info %>% +#' dplyr::mutate(logo = purrr::map(logos,magrittr::extract2,1), +#' logo = as.character(logo)) %>% +#' dplyr::select(school, logo, color, alt_color) %>% +#' dplyr::rename(team_abbr = school, +#' team_logo_espn = logo, +#' team_color = color, +#' team_color2 = alt_color) +#' +#' punts <- punts %>% dplyr::inner_join(team_info, by = c("pos_team" = "team_abbr")) +#' +#' return(punts) #' } -#' @export -college_to_pro <- function(punts, power_five = TRUE) { - - punts <- punts %>% - #dplyr::rename(season = year) %>% - dplyr::mutate(GrossYards = play_text %>% - stringr::str_extract("punt for [:digit:]+") %>% - stringr::str_extract("[:digit:]+") %>% - as.numeric()) %>% - dplyr::filter(!is.na(GrossYards)) %>% - dplyr::mutate(return_yards = as.numeric(yds_punt_return), - return_yards = ifelse(is.na(return_yards),0,return_yards), - return_yards = ifelse(stringr::str_detect(play_text,"loss"),-1*return_yards,return_yards)) %>% - #dplyr::filter(!is.na(return_yards)) %>% - dplyr::mutate(punter_player_name = play_text %>% - stringr::str_extract(".+(?= punt for)")) %>% - dplyr::mutate(YardsFromOwnEndZone = as.integer(100 - yards_to_goal)) %>% - dplyr::filter(YardsFromOwnEndZone <= 70) %>% - dplyr::mutate(touchback = play_text %>% stringr::str_detect("ouchback")) %>% - dplyr::mutate(return_yards = dplyr::if_else(touchback, 0, return_yards)) %>% - dplyr::mutate(NetYards = GrossYards - return_yards) %>% - dplyr::mutate(GrossYards = dplyr::if_else(touchback, as.numeric(GrossYards-20), as.numeric(GrossYards))) %>% - dplyr::mutate(punt_out_of_bounds = play_text %>% stringr::str_detect("out.of.bounds")) %>% - dplyr::mutate(punt_fair_catch = play_text %>% stringr::str_detect("air catch")) %>% - dplyr::mutate(punt_downed = play_text %>% stringr::str_detect("downed")) %>% - dplyr::mutate(PD = dplyr::if_else(YardsFromOwnEndZone >=41, 1, 0)) %>% - #rename columns to avoid breaking calculate_all() - dplyr::rename(ep_before_cfb = ep_before, - ep_after_cfb = ep_after) - - - - # Pull from data-repo to avoid requiring cfbd API key - #team_info <- cfbfastR::cfbd_team_info() - team_info <- readRDS(url("https://github.com/saiemgilani/cfbfastR-data/blob/master/team_info/rds/cfb_team_info_2020.rds?raw=true")) - - if(power_five) { - team_info <- team_info %>% - dplyr::filter(conference %in% c("Pac-12","SEC","Big Ten","Big 12","ACC")) - } - - team_info <- team_info %>% - dplyr::mutate(logo = purrr::map(logos,magrittr::extract2,1), - logo = as.character(logo)) %>% - dplyr::select(school, logo, color, alt_color) %>% - dplyr::rename(team_abbr = school, - team_logo_espn = logo, - team_color = color, - team_color2 = alt_color) - - punts <- punts %>% dplyr::inner_join(team_info, by = c("pos_team" = "team_abbr")) - - return(punts) -}