class: inverse, center, middle <img src="figs/RLadies_VAN/RLad_VAN.png" width="250px"/> # How Hockey Taught Me R Meghan Hall <br>
[@MeghanMHall](https://www.twitter.com/MeghanMHall) <br>
[meghan.rbind.io](https://meghan.rbind.io/) <br> R-Ladies Vancouver <br> Mar 25, 2021 --- background-image: url(figs/RLadies_VAN/RLad_VAN.png) background-position: 98% 2% background-size: 102px 115px padding-right: 10px layout: true <div class="my-footer"><span>tinyurl.com/rladies-van</span></div> --- class: middle, center # Housekeeping -- 🖥 <br>[tinyurl.com/rladies-van](https://tinyurl.com/rladies-van) <br> -- 📦 ```r devtools::install_github("meghall06/betweenthepipes") ``` <br> -- ⏰ <br> -- ❓ --- class: middle, center # The Land Before R -- 🎓 <br> the perils of Excel in a reporting-heavy office <br> -- 🏒 <br> the problem of "big" data and constantly having *more* data --- class: middle, center # Why R? -- 🔓 <br> open source = free <br> --
<br> community through `#rstats` and `R-Ladies` <br> --- class: middle, center # Post-R -- 🎓 <br> *everything* is streamlined <br> -- 🏒 <br> more analysis, more opportunities, more helping! --- class: inverse, middle, center # What you're all here for --- ## Method endorsed by multiple professionals! <br> <br> .center[<img src="figs/RLadies_VAN/tweet.png" width="585px" height="285px"/>] --- # Where does the data come from? <img src="figs/CMSAC/pipes_logo.png" width="180" style="display: block; margin: auto;" /> ```r # install the package from github devtools::install_github("meghall06/betweenthepipes") # load the libraries library(betweenthepipes) *library(tidyverse) # add the data from the package to the global environment pbp <- pbp_example ``` -- *scrape for yourself using the [Evolving Hockey](https://evolving-hockey.com/) [R scraper](https://github.com/evolvingwild/evolving-hockey/blob/master/EH_scrape_functions.R)* --- # Tonight's exercises <br> <br> <br> <br> .center[.large[**goalie save percentage**]] <br> .center[team faceoff percentage] <br> .center[individual plus-minus] --- # Goalie save percentage What is this data? How do we find saves?? -- ```r pbp %>% count(game_id, game_date, home_team, away_team) ``` <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> game_id </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> game_date </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> home_team </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> away_team </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> n </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 2019020336 </td> <td style="text-align:left;"> 2019-11-21 </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 586 </td> </tr> <tr> <td style="text-align:right;"> 2019020349 </td> <td style="text-align:left;"> 2019-11-23 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> CGY </td> <td style="text-align:right;"> 682 </td> </tr> <tr> <td style="text-align:right;"> 2019020367 </td> <td style="text-align:left;"> 2019-11-25 </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> VAN </td> <td style="text-align:right;"> 606 </td> </tr> <tr> <td style="text-align:right;"> 2019020384 </td> <td style="text-align:left;"> 2019-11-27 </td> <td style="text-align:left;"> CBJ </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 627 </td> </tr> </tbody> </table> --- # Goalie save percentage What is this data? How do we find saves?? ```r pbp %>% count(event_type) ``` <table class="table" style="font-size: 15px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_type </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> n </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> CHANGE </td> <td style="text-align:right;"> 1300 </td> </tr> <tr> <td style="text-align:left;"> FAC </td> <td style="text-align:right;"> 237 </td> </tr> <tr> <td style="text-align:left;"> SHOT </td> <td style="text-align:right;"> 211 </td> </tr> <tr> <td style="text-align:left;"> STOP </td> <td style="text-align:right;"> 190 </td> </tr> <tr> <td style="text-align:left;"> HIT </td> <td style="text-align:right;"> 134 </td> </tr> <tr> <td style="text-align:left;"> BLOCK </td> <td style="text-align:right;"> 118 </td> </tr> <tr> <td style="text-align:left;"> GIVE </td> <td style="text-align:right;"> 101 </td> </tr> <tr> <td style="text-align:left;"> MISS </td> <td style="text-align:right;"> 71 </td> </tr> <tr> <td style="text-align:left;"> TAKE </td> <td style="text-align:right;"> 44 </td> </tr> <tr> <td style="text-align:left;"> PENL </td> <td style="text-align:right;"> 25 </td> </tr> <tr> <td style="text-align:left;"> GOAL </td> <td style="text-align:right;"> 21 </td> </tr> </tbody> </table> --- # Goalie save percentage What is this data? How do we find saves?? ```r pbp %>% count(event_type) ``` <table class="table" style="font-size: 15px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_type </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> n </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> CHANGE </td> <td style="text-align:right;"> 1300 </td> </tr> <tr> <td style="text-align:left;"> FAC </td> <td style="text-align:right;"> 237 </td> </tr> <tr> <td style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> SHOT </td> <td style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> 211 </td> </tr> <tr> <td style="text-align:left;"> STOP </td> <td style="text-align:right;"> 190 </td> </tr> <tr> <td style="text-align:left;"> HIT </td> <td style="text-align:right;"> 134 </td> </tr> <tr> <td style="text-align:left;"> BLOCK </td> <td style="text-align:right;"> 118 </td> </tr> <tr> <td style="text-align:left;"> GIVE </td> <td style="text-align:right;"> 101 </td> </tr> <tr> <td style="text-align:left;"> MISS </td> <td style="text-align:right;"> 71 </td> </tr> <tr> <td style="text-align:left;"> TAKE </td> <td style="text-align:right;"> 44 </td> </tr> <tr> <td style="text-align:left;"> PENL </td> <td style="text-align:right;"> 25 </td> </tr> <tr> <td style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> GOAL </td> <td style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> 21 </td> </tr> </tbody> </table> --- # Goalie save percentage `save percentage = SHOTs / (SHOTs + GOALs)` -- ```r pbp %>% filter(event_type `%in%` c("SHOT","GOAL")) %>% select(event_type, `contains("goalie")`, event_team, event_description) ``` <table class="table" style="font-size: 14px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_type </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> home_goalie </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> away_goalie </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_team </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_description </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> CAR #20 AHO(11), Wrist, Off. Zone, 16 ft.Assists: #86 TERAVAINEN(15); #22 PESCE(6) </td> </tr> <tr> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> CAR #86 TERAVAINEN(6), Snap, Off. Zone, 32 ft.Assists: #37 SVECHNIKOV(14); #18 DZINGEL(11) </td> </tr> <tr> <td style="text-align:left;"> SHOT </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> CAR ONGOAL - #13 FOEGELE, Wrist, Off. Zone, 6 ft. </td> </tr> <tr> <td style="text-align:left;"> SHOT </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> CAR ONGOAL - #13 FOEGELE, Wrist, Off. Zone, 5 ft. </td> </tr> <tr> <td style="text-align:left;"> SHOT </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> CAR ONGOAL - #13 FOEGELE, Deflected, Off. Zone, 4 ft. </td> </tr> </tbody> </table> --- # Goalie save percentage .large[what structure is my data in?] <br> <br> *both goalies listed on the row* <br> -- .large[what structure does my data **need** to be in?] <br> <br> *the actual event goalie specified on the row* <br> -- .large[how do I get from point A to point B?] <br> <br> *create a new variable* --- # Goalie save percentage ```r pbp %>% filter(event_type %in% c("GOAL","SHOT")) %>% mutate(event_goalie = `case_when`( event_team == home_team ~ away_goalie, event_team == away_team ~ home_goalie)) %>% select(event_goalie, event_type) ``` -- <table class="table" style="font-size: 15px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_goalie </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_type </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> GOAL </td> </tr> <tr> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> GOAL </td> </tr> <tr> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> SHOT </td> </tr> <tr> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> SHOT </td> </tr> <tr> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> SHOT </td> </tr> <tr> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> SHOT </td> </tr> <tr> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> GOAL </td> </tr> <tr> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> SHOT </td> </tr> </tbody> </table> --- # Goalie save percentage ```r pbp %>% filter(event_type %in% c("GOAL","SHOT")) %>% mutate(event_goalie = case_when( event_team == home_team ~ away_goalie, event_team == away_team ~ home_goalie), save = `ifelse`(event_type == "SHOT", 1, 0)) %>% select(event_goalie, event_type, save) ``` -- <table class="table" style="font-size: 15px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_goalie </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_type </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> save </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> SHOT </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> SHOT </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> SHOT </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> SHOT </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> SHOT </td> <td style="text-align:right;"> 1 </td> </tr> </tbody> </table> --- # Goalie save percentage ```r pbp %>% filter(event_type %in% c("GOAL","SHOT")) %>% mutate(event_goalie = case_when( event_team == home_team ~ away_goalie, event_team == away_team ~ home_goalie), save = ifelse(event_type == "SHOT", 1, 0)) %>% select(event_goalie, event_type, save) %>% `group_by`(event_goalie) %>% `summarize`(saves = sum(save), all_shots = n()) %>% `arrange`(desc(all_shots)) ``` -- <table class="table" style="font-size: 15px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_goalie </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> saves </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> all_shots </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:right;"> 61 </td> <td style="text-align:right;"> 66 </td> </tr> <tr> <td style="text-align:left;"> CARTER.HART </td> <td style="text-align:right;"> 42 </td> <td style="text-align:right;"> 46 </td> </tr> <tr> <td style="text-align:left;"> DAVID.RITTICH </td> <td style="text-align:right;"> 39 </td> <td style="text-align:right;"> 41 </td> </tr> <tr> <td style="text-align:left;"> THATCHER.DEMKO </td> <td style="text-align:right;"> 32 </td> <td style="text-align:right;"> 34 </td> </tr> </tbody> </table> --- # Goalie save percentage ```r pbp %>% filter(event_type %in% c("GOAL","SHOT")) %>% mutate(event_goalie = case_when( event_team == home_team ~ away_goalie, event_team == away_team ~ home_goalie), save = ifelse(event_type == "SHOT", 1, 0)) %>% select(event_goalie, event_type, save) %>% group_by(event_goalie) %>% summarize(saves = sum(save), all_shots = n()) %>% arrange(desc(all_shots)) %>% `mutate(save_perc = saves / all_shots)` ``` -- <table class="table" style="font-size: 15px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_goalie </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> saves </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> all_shots </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> save_perc </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:right;"> 61 </td> <td style="text-align:right;"> 66 </td> <td style="text-align:right;"> 0.9242424 </td> </tr> <tr> <td style="text-align:left;"> CARTER.HART </td> <td style="text-align:right;"> 42 </td> <td style="text-align:right;"> 46 </td> <td style="text-align:right;"> 0.9130435 </td> </tr> <tr> <td style="text-align:left;"> DAVID.RITTICH </td> <td style="text-align:right;"> 39 </td> <td style="text-align:right;"> 41 </td> <td style="text-align:right;"> 0.9512195 </td> </tr> <tr> <td style="text-align:left;"> THATCHER.DEMKO </td> <td style="text-align:right;"> 32 </td> <td style="text-align:right;"> 34 </td> <td style="text-align:right;"> 0.9411765 </td> </tr> </tbody> </table> --- # Tonight's exercises <br> <br> <br> <br> .center[goalie save percentage] <br> .center[.large[**team faceoff percentage**]] <br> .center[individual plus-minus] --- # Team faceoff percentage ```r pbp %>% count(event_type) ``` <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_type </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> n </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> CHANGE </td> <td style="text-align:right;"> 1300 </td> </tr> <tr> <td style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> FAC </td> <td style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> 237 </td> </tr> <tr> <td style="text-align:left;"> SHOT </td> <td style="text-align:right;"> 211 </td> </tr> <tr> <td style="text-align:left;"> STOP </td> <td style="text-align:right;"> 190 </td> </tr> <tr> <td style="text-align:left;"> HIT </td> <td style="text-align:right;"> 134 </td> </tr> <tr> <td style="text-align:left;"> BLOCK </td> <td style="text-align:right;"> 118 </td> </tr> <tr> <td style="text-align:left;"> GIVE </td> <td style="text-align:right;"> 101 </td> </tr> <tr> <td style="text-align:left;"> MISS </td> <td style="text-align:right;"> 71 </td> </tr> <tr> <td style="text-align:left;"> TAKE </td> <td style="text-align:right;"> 44 </td> </tr> <tr> <td style="text-align:left;"> PENL </td> <td style="text-align:right;"> 25 </td> </tr> </tbody> </table> --- # Team faceoff percentage ```r pbp %>% filter(event_type == "FAC") %>% select(event_index, event_type, event_player_1, event_player_2, event_team, event_description) ``` <table class="table" style="font-size: 14px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_index </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_type </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_player_1 </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_player_2 </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_team </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_description </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> CLAUDE.GIROUX </td> <td style="text-align:left;"> SEBASTIAN.AHO </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> PHI won Neu. Zone - PHI #28 GIROUX vs CAR #20 AHO </td> </tr> <tr> <td style="text-align:right;"> 14 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> SEAN.COUTURIER </td> <td style="text-align:left;"> JORDAN.STAAL </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> PHI won Neu. Zone - PHI #14 COUTURIER vs CAR #11 STAAL </td> </tr> <tr> <td style="text-align:right;"> 23 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> KEVIN.HAYES </td> <td style="text-align:left;"> LUCAS.WALLMARK </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> PHI won Def. Zone - PHI #13 HAYES vs CAR #71 WALLMARK </td> </tr> <tr> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> SEAN.COUTURIER </td> <td style="text-align:left;"> RYAN.DZINGEL </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> PHI won Neu. Zone - PHI #14 COUTURIER vs CAR #18 DZINGEL </td> </tr> <tr> <td style="text-align:right;"> 41 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> CLAUDE.GIROUX </td> <td style="text-align:left;"> RYAN.DZINGEL </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> PHI won Def. Zone - PHI #28 GIROUX vs CAR #18 DZINGEL </td> </tr> </tbody> </table> --- # Team faceoff percentage ```r pbp %>% filter(event_type == "FAC") %>% mutate(loser = case_when(event_team == home_team ~ away_team, event_team == away_team ~ home_team)) %>% select(event_index, event_type, `winner = event_team`, loser) ``` <table class="table" style="font-size: 14px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_index </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_type </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> winner </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> loser </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> CAR </td> </tr> <tr> <td style="text-align:right;"> 14 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> CAR </td> </tr> <tr> <td style="text-align:right;"> 23 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> CAR </td> </tr> <tr> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> CAR </td> </tr> <tr> <td style="text-align:right;"> 41 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> CAR </td> </tr> <tr> <td style="text-align:right;"> 50 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> CAR </td> </tr> <tr> <td style="text-align:right;"> 60 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PHI </td> </tr> <tr> <td style="text-align:right;"> 66 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PHI </td> </tr> <tr> <td style="text-align:right;"> 71 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PHI </td> </tr> <tr> <td style="text-align:right;"> 75 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> CAR </td> </tr> </tbody> </table> --- # Team faceoff percentage .large[what structure is my data in?] <br> <br> *one row per faceoff—which is actually two data points (the winner and the loser)* <br> -- .large[what structure does my data **need** to be in?] <br> <br> *one row per each **team** in the faceoff (one row per data point!)* <br> -- .large[how do I get from point A to point B?] <br> <br> *pivot!* --- # Team Faceoff Percentage .center[<img src="figs/RLadies_VAN/pivot.png" width="574px" height="236px"/>] .center[*[from R for Data Science](https://r4ds.had.co.nz/)*] -- .center[<img src="figs/RLadies_VAN/pivot2.png" width="380px" height="202px"/>] --- # Team faceoff percentage ```r pbp %>% filter(event_type == "FAC") %>% mutate(loser = case_when(event_team == home_team ~ away_team, event_team == away_team ~ home_team)) %>% select(event_index, event_type, winner = event_team, loser) %>% * pivot_longer(winner:loser, * names_to = "result", values_to = "team") ``` <table class="table" style="font-size: 13px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_index </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_type </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> result </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> team </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> winner </td> <td style="text-align:left;"> PHI </td> </tr> <tr> <td style="text-align:right;"> 7 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> loser </td> <td style="text-align:left;"> CAR </td> </tr> <tr> <td style="text-align:right;"> 14 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> winner </td> <td style="text-align:left;"> PHI </td> </tr> <tr> <td style="text-align:right;"> 14 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> loser </td> <td style="text-align:left;"> CAR </td> </tr> <tr> <td style="text-align:right;"> 23 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> winner </td> <td style="text-align:left;"> PHI </td> </tr> <tr> <td style="text-align:right;"> 23 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> loser </td> <td style="text-align:left;"> CAR </td> </tr> <tr> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> winner </td> <td style="text-align:left;"> PHI </td> </tr> <tr> <td style="text-align:right;"> 37 </td> <td style="text-align:left;"> FAC </td> <td style="text-align:left;"> loser </td> <td style="text-align:left;"> CAR </td> </tr> </tbody> </table> --- # Team faceoff percentage ```r pbp %>% filter(event_type == "FAC") %>% mutate(loser = case_when(event_team == home_team ~ away_team, event_team == away_team ~ home_team)) %>% select(event_index, event_type, winner = event_team, loser) %>% pivot_longer(winner:loser, names_to = "result", values_to = "team") %>% * mutate(win = ifelse(result == "winner", 1, 0)) %>% * group_by(team) %>% * summarize(wins = sum(win), * faceoffs = n()) %>% * mutate(fo_perc = wins / faceoffs) ``` <table class="table" style="font-size: 13px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> team </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> wins </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> faceoffs </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> fo_perc </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 135 </td> <td style="text-align:right;"> 237 </td> <td style="text-align:right;"> 0.5696203 </td> </tr> <tr> <td style="text-align:left;"> CGY </td> <td style="text-align:right;"> 30 </td> <td style="text-align:right;"> 74 </td> <td style="text-align:right;"> 0.4054054 </td> </tr> <tr> <td style="text-align:left;"> CAR </td> <td style="text-align:right;"> 27 </td> <td style="text-align:right;"> 59 </td> <td style="text-align:right;"> 0.4576271 </td> </tr> <tr> <td style="text-align:left;"> CBJ </td> <td style="text-align:right;"> 25 </td> <td style="text-align:right;"> 52 </td> <td style="text-align:right;"> 0.4807692 </td> </tr> </tbody> </table> --- # Tonight's exercises <br> <br> <br> <br> .center[goalie save percentage] <br> .center[team faceoff percentage] <br> .center[.large[**individual plus-minus**]] --- # Individual plus-minus .center[<img src="figs/RLadies_VAN/plusminus.jpg" width="540px" height="462px"/>] --- # Individual plus-minus ```r pbp %>% filter(game_strength_state == "5v5" & event_type == "GOAL") %>% select(event_index, event_type, event_team, home_team, away_team, home_goalie, away_goalie, contains("_on_")) ``` <table class="table" style="font-size: 12px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_index </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_type </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_team </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> home_team </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> away_team </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> home_on_1 </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> home_on_2 </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> away_on_1 </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> away_on_2 </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> ANDREI.SVECHNIKOV </td> <td style="text-align:left;"> BRETT.PESCE </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> CLAUDE.GIROUX </td> </tr> <tr> <td style="text-align:right;"> 430 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> BRETT.PESCE </td> <td style="text-align:left;"> BROCK.MCGINN </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> CLAUDE.GIROUX </td> </tr> <tr> <td style="text-align:right;"> 503 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> ANDREI.SVECHNIKOV </td> <td style="text-align:left;"> BRETT.PESCE </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> CLAUDE.GIROUX </td> </tr> <tr> <td style="text-align:right;"> 364 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> CGY </td> <td style="text-align:left;"> CARTER.HART </td> <td style="text-align:left;"> JAKUB.VORACEK </td> <td style="text-align:left;"> BRANDON.DAVIDSON </td> <td style="text-align:left;"> DAVID.RITTICH </td> </tr> <tr> <td style="text-align:right;"> 433 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CGY </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> CGY </td> <td style="text-align:left;"> CARTER.HART </td> <td style="text-align:left;"> CLAUDE.GIROUX </td> <td style="text-align:left;"> ANDREW.MANGIAPANE </td> <td style="text-align:left;"> DAVID.RITTICH </td> </tr> <tr> <td style="text-align:right;"> 587 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> CGY </td> <td style="text-align:left;"> CARTER.HART </td> <td style="text-align:left;"> JOEL.FARABEE </td> <td style="text-align:left;"> DAVID.RITTICH </td> <td style="text-align:left;"> DEREK.RYAN </td> </tr> <tr> <td style="text-align:right;"> 123 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> VAN </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> VAN </td> <td style="text-align:left;"> CARTER.HART </td> <td style="text-align:left;"> JAMES.VAN RIEMSDYK </td> <td style="text-align:left;"> BO.HORVAT </td> <td style="text-align:left;"> J.T..MILLER </td> </tr> <tr> <td style="text-align:right;"> 257 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> VAN </td> <td style="text-align:left;"> CARTER.HART </td> <td style="text-align:left;"> JAKUB.VORACEK </td> <td style="text-align:left;"> ALEX.EDLER </td> <td style="text-align:left;"> BO.HORVAT </td> </tr> <tr> <td style="text-align:right;"> 509 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> VAN </td> <td style="text-align:left;"> CARTER.HART </td> <td style="text-align:left;"> JAKUB.VORACEK </td> <td style="text-align:left;"> CHRIS.TANEV </td> <td style="text-align:left;"> ELIAS.PETTERSSON </td> </tr> <tr> <td style="text-align:right;"> 60 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CBJ </td> <td style="text-align:left;"> CBJ </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> DEAN.KUKAN </td> <td style="text-align:left;"> EMIL.BEMSTROM </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> CLAUDE.GIROUX </td> </tr> </tbody> </table> --- # Team faceoff percentage .large[what structure is my data in?] <br> <br> *one row per goal—which contains **many** data points* <br> -- .large[what structure does my data **need** to be in?] <br> <br> *one row per each player on the ice* <br> -- .large[how do I get from point A to point B?] <br> <br> *pivot again, with a twist* --- # Individual plus-minus ```r pbp %>% filter(game_strength_state == "5v5" & event_type == "GOAL") %>% select(event_index, event_type, event_team, home_team, away_team, home_goalie, away_goalie, contains("_on_")) %>% * rename_at(vars(contains("_on_")), ~str_replace(., "on_", "")) ``` <table class="table" style="font-size: 12px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_index </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_type </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_team </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> home_team </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> away_team </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> home_goalie </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> away_goalie </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> home_1 </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> home_2 </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> away_1 </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> away_2 </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> ANDREI.SVECHNIKOV </td> <td style="text-align:left;"> BRETT.PESCE </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> CLAUDE.GIROUX </td> </tr> <tr> <td style="text-align:right;"> 430 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> BRETT.PESCE </td> <td style="text-align:left;"> BROCK.MCGINN </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> CLAUDE.GIROUX </td> </tr> <tr> <td style="text-align:right;"> 503 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> ANDREI.SVECHNIKOV </td> <td style="text-align:left;"> BRETT.PESCE </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> CLAUDE.GIROUX </td> </tr> <tr> <td style="text-align:right;"> 364 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> CGY </td> <td style="text-align:left;"> CARTER.HART </td> <td style="text-align:left;"> DAVID.RITTICH </td> <td style="text-align:left;"> CARTER.HART </td> <td style="text-align:left;"> JAKUB.VORACEK </td> <td style="text-align:left;"> BRANDON.DAVIDSON </td> <td style="text-align:left;"> DAVID.RITTICH </td> </tr> <tr> <td style="text-align:right;"> 433 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CGY </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> CGY </td> <td style="text-align:left;"> CARTER.HART </td> <td style="text-align:left;"> DAVID.RITTICH </td> <td style="text-align:left;"> CARTER.HART </td> <td style="text-align:left;"> CLAUDE.GIROUX </td> <td style="text-align:left;"> ANDREW.MANGIAPANE </td> <td style="text-align:left;"> DAVID.RITTICH </td> </tr> </tbody> </table> --- # Individual plus-minus ```r pbp %>% filter(game_strength_state == "5v5" & event_type == "GOAL") %>% select(event_index, event_type, event_team, home_team, away_team, home_goalie, away_goalie, contains("_on_")) %>% rename_at(vars(contains("_on_")), ~str_replace(., "on_", "")) %>% * pivot_longer(-c(starts_with("event")), * names_to = c("home_away", ".value"), names_sep = "_") ``` <table class="table" style="font-size: 12px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_index </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_type </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_team </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> home_away </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> team </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> goalie </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> 1 </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> 2 </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> 3 </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> home </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> ANDREI.SVECHNIKOV </td> <td style="text-align:left;"> BRETT.PESCE </td> <td style="text-align:left;"> JOEL.EDMUNDSON </td> </tr> <tr> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> away </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> CLAUDE.GIROUX </td> <td style="text-align:left;"> MATT.NISKANEN </td> </tr> <tr> <td style="text-align:right;"> 430 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> home </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> BRETT.PESCE </td> <td style="text-align:left;"> BROCK.MCGINN </td> <td style="text-align:left;"> JOEL.EDMUNDSON </td> </tr> <tr> <td style="text-align:right;"> 430 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> away </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> BRIAN.ELLIOTT </td> <td style="text-align:left;"> CLAUDE.GIROUX </td> <td style="text-align:left;"> MORGAN.FROST </td> </tr> <tr> <td style="text-align:right;"> 503 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> PHI </td> <td style="text-align:left;"> home </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> ANDREI.SVECHNIKOV </td> <td style="text-align:left;"> BRETT.PESCE </td> <td style="text-align:left;"> JOEL.EDMUNDSON </td> </tr> </tbody> </table> --- # Individual plus-minus ```r pbp %>% filter(game_strength_state == "5v5" & event_type == "GOAL") %>% select(event_index, event_type, event_team, home_team, away_team, home_goalie, away_goalie, contains("_on_")) %>% rename_at(vars(contains("_on_")), ~str_replace(., "on_", "")) %>% pivot_longer(-c(starts_with("event")), names_to = c("home_away", ".value"), names_sep = "_") %>% * pivot_longer(`1`:`7`, names_to = NULL, values_to = "player", * values_drop_na = TRUE) ``` <table class="table" style="font-size: 13px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_index </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_type </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_team </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> home_away </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> team </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> goalie </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> player </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> home </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> ANDREI.SVECHNIKOV </td> </tr> <tr> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> home </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> BRETT.PESCE </td> </tr> <tr> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> home </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> JOEL.EDMUNDSON </td> </tr> <tr> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> home </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> PETR.MRAZEK </td> </tr> <tr> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> home </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> SEBASTIAN.AHO </td> </tr> </tbody> </table> --- # Individual plus-minus ```r pbp %>% filter(game_strength_state == "5v5" & event_type == "GOAL") %>% select(event_index, event_type, event_team, home_team, away_team, home_goalie, away_goalie, contains("_on_")) %>% rename_at(vars(contains("_on_")), ~str_replace(., "on_", "")) %>% pivot_longer(-c(starts_with("event")), names_to = c("home_away", ".value"), names_sep = "_") %>% pivot_longer(`1`:`7`, names_to = NULL, values_to = "player", values_drop_na = TRUE) %>% * filter(player != goalie) %>% * mutate(plus_minus = ifelse(event_team == team, 1, -1)) ``` <table class="table" style="font-size: 13px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_index </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_type </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> event_team </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> home_away </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> team </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> goalie </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> player </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> plus_minus </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> home </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> ANDREI.SVECHNIKOV </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> home </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> BRETT.PESCE </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> home </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> JOEL.EDMUNDSON </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> home </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> SEBASTIAN.AHO </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:right;"> 34 </td> <td style="text-align:left;"> GOAL </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> home </td> <td style="text-align:left;"> CAR </td> <td style="text-align:left;"> PETR.MRAZEK </td> <td style="text-align:left;"> TEUVO.TERAVAINEN </td> <td style="text-align:right;"> 1 </td> </tr> </tbody> </table> --- # Individual plus-minus ```r pbp %>% filter(game_strength_state == "5v5" & event_type == "GOAL") %>% select(event_index, event_type, event_team, home_team, away_team, home_goalie, away_goalie, contains("_on_")) %>% rename_at(vars(contains("_on_")), ~str_replace(., "on_", "")) %>% pivot_longer(-c(starts_with("event")), names_to = c("home_away", ".value"), names_sep = "_") %>% pivot_longer(`1`:`7`, names_to = NULL, values_to = "player", values_drop_na = TRUE) %>% filter(player != goalie) %>% mutate(plus_minus = ifelse(event_team == team, 1, -1)) %>% * group_by(player, team) %>% * summarize(plus_minus = sum(plus_minus)) %>% * arrange(desc(abs(plus_minus))) ``` <table class="table" style="font-size: 13px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> player </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #5C164E !important;"> team </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #5C164E !important;"> plus_minus </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> CLAUDE.GIROUX </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> -3 </td> </tr> <tr> <td style="text-align:left;"> JAKUB.VORACEK </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 3 </td> </tr> <tr> <td style="text-align:left;"> JUSTIN.BRAUN </td> <td style="text-align:left;"> PHI </td> <td style="text-align:right;"> 3 </td> </tr> </tbody> </table> --- # Even more exercises <br> .large[which players got the most points in these games?] <br> <br> <br> .large[how many standings points did each team get per game?] <br> <br> <br> .large[which player had the highest average time on ice?] <br> <br> <br> *all code available [here](https://github.com/meghall06/personal-website/blob/master/static/RLadies_VAN_code.R)* --- # Tiny tips for success .large[code, code, code!] <br> <br> *a little bit each day, constantly iterate* <br> -- .large[use data you find interesting] <br> <br> *[Data is Plural](https://www.data-is-plural.com/) is a weekly newsletter with interesting datasets*<br> *[nflfastr](https://github.com/mrcaseb/nflfastR), [baseballr](http://billpetti.github.io/baseballr/)* <br> -- .large[focus first on the problems you have] <br> <br> *RMarkdown templates, ggplot themes, internal packages* --- class: center, middle, inverse # Thank you! Slides created via the R package [xaringan](https://github.com/yihui/xaringan). <br>
[@MeghanMHall](https://www.twitter.com/MeghanMHall)