Skip to content

Instantly share code, notes, and snippets.

@tonyelhabr
Created March 29, 2022 14:21
Show Gist options
  • Save tonyelhabr/e46656d5d717539167ed5a2751e4dce7 to your computer and use it in GitHub Desktop.
Save tonyelhabr/e46656d5d717539167ed5a2751e4dce7 to your computer and use it in GitHub Desktop.
Standardized Opta Goal Mouth Coordinates
library(readr)
library(dplyr)
library(devtools)
shots <- read_csv('shots_1549727.csv')
glimpse(shots)
#> Rows: 22
#> Columns: 14
#> $ season_id         <dbl> 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021~
#> $ game_id           <dbl> 1549727, 1549727, 1549727, 1549727, 1549727, 1549727~
#> $ period_id         <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2~
#> $ expanded_minute   <dbl> 5, 7, 19, 19, 28, 32, 34, 38, 40, 49, 50, 51, 57, 58~
#> $ second            <dbl> 7, 38, 22, 28, 15, 37, 39, 40, 33, 37, 11, 2, 43, 42~
#> $ team_name         <chr> "Southampton", "Southampton", "West Ham", "West Ham"~
#> $ player_name       <chr> "Oriol Romeu", "Mohamed Elyounoussi", "Craig Dawson"~
#> $ type_name         <chr> "MissedShots", "Goal", "SavedShot", "SavedShot", "Sa~
#> $ outcome_type_name <chr> "Successful", "Successful", "Successful", "Successfu~
#> $ is_shot           <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE~
#> $ x                 <dbl> 75.4, 81.1, 95.3, 74.1, 87.3, 85.3, 72.4, 80.9, 96.9~
#> $ y                 <dbl> 44.7, 52.4, 49.7, 64.3, 82.4, 47.2, 52.8, 55.5, 69.7~
#> $ goal_mouth_y      <dbl> 40.2, 47.1, 52.1, 48.6, 47.1, 50.8, 50.6, 47.8, 1.5,~
#> $ goal_mouth_z      <dbl> 30.6, 9.5, 11.1, 19.0, 19.0, 3.8, 4.4, 0.6, 4.2, 1.3~

source_url('https://raw.githubusercontent.com/larsmaurath/keine-mathematik/master/content/data/standardize_coordinates.R')
#> i SHA-1 hash of file is 98865bbe8da988de43ee53cc22edde76728a6641

shots_standardized <- shots %>% 
  standardize_opta_x(cols = 'x') %>% 
  standardize_opta_y(cols = c('y', 'goal_mouth_y')) %>% 
  standardize_opta_z(cols = 'goal_mouth_z')
glimpse(shots_standardized)
#> Rows: 22
#> Columns: 14
#> $ season_id         <dbl> 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021~
#> $ game_id           <dbl> 1549727, 1549727, 1549727, 1549727, 1549727, 1549727~
#> $ period_id         <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2~
#> $ expanded_minute   <dbl> 5, 7, 19, 19, 28, 32, 34, 38, 40, 49, 50, 51, 57, 58~
#> $ second            <dbl> 7, 38, 22, 28, 15, 37, 39, 40, 33, 37, 11, 2, 43, 42~
#> $ team_name         <chr> "Southampton", "Southampton", "West Ham", "West Ham"~
#> $ player_name       <chr> "Oriol Romeu", "Mohamed Elyounoussi", "Craig Dawson"~
#> $ type_name         <chr> "MissedShots", "Goal", "SavedShot", "SavedShot", "Sa~
#> $ outcome_type_name <chr> "Successful", "Successful", "Successful", "Successfu~
#> $ is_shot           <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE~
#> $ x                 <dbl> 80.20909, 86.42727, 100.06500, 78.79091, 92.73556, 9~
#> $ y                 <dbl> 30.32212, 35.83000, 33.77125, 43.97536, 56.03200, 31~
#> $ goal_mouth_y      <dbl> 27.19939, 31.78875, 35.60125, 32.93250, 31.78875, 34~
#> $ goal_mouth_z      <dbl> 1.96484211, 0.61000000, 0.71273684, 1.22000000, 1.22~

Created on 2022-03-29 by the reprex package (v2.0.1)

season_id game_id period_id expanded_minute second team_name player_name type_name outcome_type_name is_shot x y goal_mouth_y goal_mouth_z
2021 1549727 1 5 7 Southampton Oriol Romeu MissedShots Successful TRUE 75.4 44.7 40.2 30.6
2021 1549727 1 7 38 Southampton Mohamed Elyounoussi Goal Successful TRUE 81.1 52.4 47.1 9.5
2021 1549727 1 19 22 West Ham Craig Dawson SavedShot Successful TRUE 95.3 49.7 52.1 11.1
2021 1549727 1 19 28 West Ham Declan Rice SavedShot Successful TRUE 74.1 64.3 48.6 19
2021 1549727 1 28 15 Southampton Theo Walcott SavedShot Successful TRUE 87.3 82.4 47.1 19
2021 1549727 1 32 37 Southampton Mohamed Elyounoussi SavedShot Successful TRUE 85.3 47.2 50.8 3.8
2021 1549727 1 34 39 Southampton Nathan Redmond SavedShot Successful TRUE 72.4 52.8 50.6 4.4
2021 1549727 1 38 40 West Ham Nikola Vlasic SavedShot Successful TRUE 80.9 55.5 47.8 0.6
2021 1549727 1 40 33 Southampton Armando Broja MissedShots Successful TRUE 96.9 69.7 1.5 4.2
2021 1549727 2 49 37 West Ham Michail Antonio SavedShot Successful TRUE 87.6 64.1 50.6 1.3
2021 1549727 2 50 11 Southampton Nathan Redmond SavedShot Successful TRUE 77.5 37.3 47.8 1.3
2021 1549727 2 51 2 West Ham Michail Antonio Goal Successful TRUE 96.6 50.7 50.3 27.2
2021 1549727 2 57 43 Southampton Kyle Walker-Peters SavedShot Successful TRUE 86.5 66.6 52.4 19
2021 1549727 2 58 42 West Ham Declan Rice MissedShots Successful TRUE 72 37.7 56.6 15.3
2021 1549727 2 63 12 Southampton James Ward-Prowse Goal Successful TRUE 88.5 50 52.9 10.8
2021 1549727 2 66 58 West Ham Saïd Benrahma Goal Successful TRUE 91.6 51.8 54.1 19.6
2021 1549727 2 72 7 Southampton Jan Bednarek Goal Successful TRUE 90.2 52.6 46.1 14.6
2021 1549727 2 74 7 West Ham Tomas Soucek SavedShot Successful TRUE 94.7 52.4 51.2 19
2021 1549727 2 81 5 West Ham Saïd Benrahma MissedShots Successful TRUE 77.2 40 36 97.2
2021 1549727 2 92 5 West Ham Michail Antonio SavedShot Successful TRUE 91.8 46.1 46.7 19
2021 1549727 2 93 17 West Ham Arthur Masuaku MissedShots Successful TRUE 72.4 57.5 59.7 94.4
2021 1549727 2 96 13 Southampton Stuart Armstrong SavedShot Successful TRUE 75.9 48 49.5 19
@tonyelhabr
Copy link
Author

tonyelhabr commented Mar 29, 2022

You can look through shots on Fotmob as a visual check. Goal coordinates are now in meters, so a shot with goal_mouth_z<=2.44 is below the crossbar. A shot with goal_mouth_y>=34+7.32/2 and goal_mouth_y<=34-7.32/2 is in the width of the frame.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment