soccerFlipDirection: Flips x,y-coordinates horizontally in one half to account for...

Description Usage Arguments Value Examples

View source: R/soccerFlipDirection.R

Description

Normalises direction of attack in both halves of both teams by flipping x,y-coordinates horizontally in either the first or second half; i.e. teams attack in the same direction all game despite changing sides at half-time.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
soccerFlipDirection(
  df,
  lengthPitch = 105,
  widthPitch = 68,
  teamToFlip = NULL,
  periodToFlip = 1:2,
  team = "team",
  period = "period",
  x = "x",
  y = "y"
)

Arguments

df

dataframe containing unnormalised x,y-coordinates

lengthPitch, widthPitch

length, width of pitch in metres

teamToFlip

character, name of team to flip. If NULL, all x,y-coordinates in df will be flipped

periodToFlip

integer, period(s) to flip

team

character, name of variables containing x,y-coordinates

period

character, name of variable containing period labels

x, y

character, name of variables containing x,y-coordinates

Value

a dataframe

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
library(dplyr)

# flip x,y-coords of France in both halves of statsbomb data
data(statsbomb)
statsbomb %>% 
  soccerFlipDirection(team = "team.name", x = "location.x", y = "location.y",
                      teamToFlip = "France")
  
# flip x,y-coords in 2nd half of Tromso, based on a dummy period variable
data(tromso)
tromso %>% 
  mutate(period = if_else(t > as.POSIXct("2013-11-07 21:14:00 GMT"), 1, 2)) %>% 
  soccerFlipDirection(periodToFlip = 2)
  

JoGall/soccermatics documentation built on Aug. 12, 2021, 1:20 p.m.