Visualizing Soccer with StatsBomb Data and R, Part 1: Simple xG and Pass Partner Plots!

[This article was first published on R by R(yo), and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This will be Part 1 of what I hope to be a multi-part series of
plotting soccer event-level data with R! This is more of a tutorial blog
post rather than a deep analytical piece but I will give some context to
the examples to set the scene! I can’t give an exact number of how many
parts as I am still getting to grips with this kind of data and I feel
like I’ve only scratched the surface. You can read some of the other
stuff I’ve done, preview blog posts for the Asian
Cup
and the
Copa
America
,
along with the code to all the standalone viz I’ve done on my
soccer_ggplot GitHub
repository
.

I’ll mostly be using the Messi Data Biography data but the steps I
show below are applicable to the other data available as well! I will be
working with the free data sets so some things may differ compared
to the full data available. Also note that it is possible to create the
viz in this blog post using data from other providers of event-level
data such as Opta. The difference in code will mainly be in the data
ingestion and cleaning phases but the gist of the {ggplot2} code should
be similar.

As an example and motivation, one of the visualizations we are going to
create is shown below:

Let’s get started!

Getting the Data

A few important steps before you even start using R:

Once that’s done we can start coding!

Packages

Here’s all the packages I’ll be using (note: I like using {pacman} so I
don’t have to repeat library() a billion times):

<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">require</span><span class="p">(</span><span class="s2">"pacman"</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="n">install.packages</span><span class="p">(</span><span class="s2">"pacman"</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">pacman</span><span class="o">::</span><span class="n">p_load</span><span class="p">(</span><span class="n">tidyverse</span><span class="p">,</span><span class="w"> </span><span class="c1">## mainly dplyr, purrr, and tidyr</span><span class="w">
               </span><span class="n">StatsBombR</span><span class="p">,</span><span class="w"> </span><span class="n">SBpitch</span><span class="p">,</span><span class="w"> </span><span class="n">soccermatics</span><span class="p">,</span><span class="w">
               </span><span class="n">extrafont</span><span class="p">,</span><span class="w"> </span><span class="n">ggupset</span><span class="p">,</span><span class="w"> </span><span class="n">tibbletime</span><span class="p">,</span><span class="w">
               </span><span class="n">ggtext</span><span class="p">,</span><span class="w"> </span><span class="n">ggrepel</span><span class="p">,</span><span class="w"> </span><span class="n">glue</span><span class="p">,</span><span class="w">
               </span><span class="n">patchwork</span><span class="p">,</span><span class="w"> </span><span class="n">cowplot</span><span class="p">,</span><span class="w"> </span><span class="n">gtable</span><span class="p">,</span><span class="w"> </span><span class="n">grid</span><span class="p">,</span><span class="w">
               </span><span class="n">magick</span><span class="p">)</span><span class="w">

</span><span class="c1">## loading fonts</span><span class="w">
</span><span class="n">loadfonts</span><span class="p">(</span><span class="n">device</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"win"</span><span class="p">,</span><span class="w"> </span><span class="n">quiet</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w">
</span>

After loading the
{StatsBombR} library (note:
that I already did this above but just showing it again below for
demonstrational purposes) we first want to take a look at the output of
the FreeCompetitions() function which gives you a data frame of all
the competitions available for free from StatsBomb. Do note that this
part will be different if you are a customer using the API.

<span class="n">library</span><span class="p">(</span><span class="n">StatsBombR</span><span class="p">)</span><span class="w">
</span><span class="n">comps</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">FreeCompetitions</span><span class="p">()</span><span class="w">

</span><span class="n">glimpse</span><span class="p">(</span><span class="n">comps</span><span class="p">)</span><span class="w">
</span>

If you View() or glimpse() the data frame you’ll see that the
competition_id we need is 11 for the Lionel Messi data. We use
this to filter() the comps data frame and then call FreeMatches()
to get a data frame of the available matches. Finally pass that data
frame to StatsBombFreeEvents() to access the data, this can take a
while if you don’t have a good internet connection!

<span class="n">messi_matches_raw</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">comps</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">filter</span><span class="p">(</span><span class="n">competition_id</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">11</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">FreeMatches</span><span class="p">()</span><span class="w">

</span><span class="n">messi_data_raw</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">StatsBombFreeEvents</span><span class="p">(</span><span class="n">MatchesDF</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">messi_matches_raw</span><span class="p">)</span><span class="w">
</span>

Clean All and Add Season Labels

Now that we’ve got the raw data we can clean it and add some extra
information using the allclean() function. This function takes care
of:

  • cleanlocations(): cleans the location variables in the data
  • Goalkeeper: Add goalkeeper data from the freeze frame
  • Shot: Adds more shot information
  • Freeze frame: Extracts info from freeze frames, i.e. density
  • Defensive: Defensive information

We can also add in the actual season names by joining with the “comps”
data frame and joining it by the “season_id”.

<span class="n">messi_data_clean</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">messi_data_raw</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">allclean</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w">  
  </span><span class="n">left_join</span><span class="p">(</span><span class="n">comps</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">select</span><span class="p">(</span><span class="n">season_id</span><span class="p">,</span><span class="w"> </span><span class="n">season_name</span><span class="p">),</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"season_id"</span><span class="p">)</span><span class="w">
</span>

The player names in the data are the full names and for lots of
Spanish/Portuguese players in the data that means their FULL names.
To make the names shorter and so that labels on plots can be more
legible it’s a good idea to clean the “name” variables up a bit. There
is a function, JoinPlayerNickName() that allows you to do that,
however, you need a username and password for the StatsBomb API, which I
don’t have sooo… I have several options:

  • Manually clean the names…
  • Find a nice list of player names and left_join() after cleaning
    • Example: Use transfermarkt data
  • Use the {fuzzyjoin} package:
    Join a name even if there are n number of differences

In the end I just did it manually… around 10 full minutes of hard
concentration and it was done. Added bonus is that now I am intimately
familiar with the full names of every Barcelona player in the past
decade!

<span class="n">messi_data_clean</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">messi_data_clean</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="c1">## player name</span><span class="w">
  </span><span class="n">mutate</span><span class="p">(</span><span class="n">player.name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">case_when</span><span class="p">(</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Oleguer Presas Renom"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Oleguer"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Xavier Hernández Creus"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Xavi"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Carles Puyol i Saforcada"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Carles Puyol"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Anderson Luís de Souza"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Deco"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Rafael Márquez Álvarez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Rafa Márquez"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Giovanni van Bronckhorst"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Gio v.Bronckhorst"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Samuel Eto'o Fils"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Samuel Eto'o"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Víctor Valdés Arribas"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Víctor Valdés"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Juliano Haus Belletti"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Juliano Belletti"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Ludovic Giuly"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Ludovic Giuly"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Andrés Iniesta Luján"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Andrés Iniesta"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Ronaldo de Assis Moreira"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Ronaldinho"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Lionel Andrés Messi Cuccittini"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Lionel Messi"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Fernando Navarro i Corbacho"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Fernando Navarro"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Sylvio Mendes Campos Junior"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Sylvinho"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Damià Abella Pérez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Damià"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Rubén Iván Martínez Andrade"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Ronaldinho"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Ronaldo de Assis Moreira"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Rubén"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Thiago Motta"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Thiago Motta"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Mark van Bommel"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Mark van Bommel"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Henrik Larsson"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Henrik Larsson"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"José Edmílson Gomes de Moraes"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Edmílson"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Gabriel Francisco García de la Torre"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Gabri"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Santiago Ezquerro Marín"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Santi Ezquerro"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Maximiliano Gastón López"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Maxi López"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Gianluca Zambrotta"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Gianluca Zambrotta"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Eiður Smári Guðjohnsen"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Eiður Guðjohnsen"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Lilian Thuram"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Lilian Thuram"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Javier Pedro Saviola Fernández"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Javier Saviola"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Gnégnéri Yaya Touré"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Yaya Touré"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Bojan Krkíc Pérez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Bojan"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Eric-Sylvain Bilal Abidal"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Eric Abidal"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Gabriel Alejandro Milito"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Gabriel Milito"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Giovani dos Santos Ramírez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Giovani dos Santos"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Víctor Vázquez Solsona"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Víctor Vázquez"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Thierry Henry"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Thierry Henry"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"José Manuel Pinto Colorado"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"José Manuel Pinto"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Daniel Alves da Silva"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Dani Alves"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Sergio Busquets i Burgos"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Sergio Busquets"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Seydou Kéita"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Seydou Kéita"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"José Martín Cáceres Silva"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Martín Cáceres"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Gerard Piqué Bernabéu"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Gerard Piqué"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Aliaksandr Hleb"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Aliaksandr Hleb"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Pedro Eliezer Rodríguez Ledesma"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Pedro"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Sergio Rodríguez García"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Rodri"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Rafael Romero Serrano"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Fali"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"José Manuel Rueda Sampedro"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"José Manuel Rueda"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Zlatan Ibrahimovic"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Zlatan Ibrahimovic"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Dmytro Chygrynskiy"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Dmytro Chygrynskiy"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Maxwell Scherrer Cabelino Andrade"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Maxwell"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Jeffren Isaac Suárez Bermúdez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Jeffren"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Víctor Sánchez Mata"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Víctor Sánchez"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Thiago Alcântara do Nascimento"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Thiago Alcântara"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"David Villa Sánchez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"David Villa"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Javier Alejandro Mascherano"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Javier Mascherano"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Andreu Fontàs Prat"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Andreu Fontàs"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Ibrahim Afellay"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Ibrahim Afellay"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Manuel Agudo Durán"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Nolito"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Marc Bartra Aregall"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Marc Bartra"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Adriano Correia Claro"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Adriano"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Martín Montoya Torralbo"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Martín Montoya"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Jonathan dos Santos Ramírez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Jonathan dos Santos"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Francesc Fàbregas i Soler"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Cesc Fàbregas"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Alexis Alejandro Sánchez Sánchez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Alexis Sánchez"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Juan Isaac Cuenca López"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Isaac Cuenca"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Gerard Deulofeu Lázaro"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Gerard Deulofeu"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Cristian Tello"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Cristian Tello"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Sergi Roberto Carnicer"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Sergi Roberto"</span><span class="p">,</span><span class="w">
    </span><span class="n">player.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Marc Muniesa Martínez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Marc Muniesa"</span><span class="p">,</span><span class="w">
    </span><span class="kc">TRUE</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="n">player.name</span><span class="w">
  </span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="c1">## pass.recipient.name</span><span class="w">
  </span><span class="n">mutate</span><span class="p">(</span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">case_when</span><span class="p">(</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Oleguer Presas Renom"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Oleguer"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Xavier Hernández Creus"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Xavi"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Carles Puyol i Saforcada"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Carles Puyol"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Anderson Luís de Souza"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Deco"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Rafael Márquez Álvarez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Rafa Márquez"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Giovanni van Bronckhorst"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Gio v.Bronckhorst"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Samuel Eto'o Fils"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Samuel Eto'o"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Víctor Valdés Arribas"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Víctor Valdés"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Juliano Haus Belletti"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Juliano Belletti"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Ludovic Giuly"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Ludovic Giuly"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Andrés Iniesta Luján"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Andrés Iniesta"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Ronaldo de Assis Moreira"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Ronaldinho"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Lionel Andrés Messi Cuccittini"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Lionel Messi"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Fernando Navarro i Corbacho"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Fernando Navarro"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Sylvio Mendes Campos Junior"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Sylvinho"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Damià Abella Pérez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Damià"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Rubén Iván Martínez Andrade"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Ronaldinho"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Ronaldo de Assis Moreira"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Rubén"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Thiago Motta"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Thiago Motta"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Mark van Bommel"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Mark van Bommel"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Henrik Larsson"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Henrik Larsson"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"José Edmílson Gomes de Moraes"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Edmílson"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Gabriel Francisco García de la Torre"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Gabri"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Santiago Ezquerro Marín"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Santi Ezquerro"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Maximiliano Gastón López"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Maxi López"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Gianluca Zambrotta"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Gianluca Zambrotta"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Eiður Smári Guðjohnsen"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Eiður Guðjohnsen"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Lilian Thuram"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Lilian Thuram"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Javier Pedro Saviola Fernández"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Javier Saviola"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Gnégnéri Yaya Touré"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Yaya Touré"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Bojan Krkíc Pérez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Bojan"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Eric-Sylvain Bilal Abidal"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Eric Abidal"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Gabriel Alejandro Milito"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Gabriel Milito"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Giovani dos Santos Ramírez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Giovani dos Santos"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Víctor Vázquez Solsona"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Víctor Vázquez"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Thierry Henry"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Thierry Henry"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"José Manuel Pinto Colorado"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"José Manuel Pinto"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Daniel Alves da Silva"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Dani Alves"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Sergio Busquets i Burgos"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Sergio Busquets"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Seydou Kéita"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Seydou Kéita"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"José Martín Cáceres Silva"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Martín Cáceres"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Gerard Piqué Bernabéu"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Gerard Piqué"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Aliaksandr Hleb"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Aliaksandr Hleb"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Pedro Eliezer Rodríguez Ledesma"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Pedro"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Sergio Rodríguez García"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Rodri"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Rafael Romero Serrano"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Fali"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"José Manuel Rueda Sampedro"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"José Manuel Rueda"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Zlatan Ibrahimovic"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Zlatan Ibrahimovic"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Dmytro Chygrynskiy"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Dmytro Chygrynskiy"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Maxwell Scherrer Cabelino Andrade"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Maxwell"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Jeffren Isaac Suárez Bermúdez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Jeffren"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Víctor Sánchez Mata"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Víctor Sánchez"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Thiago Alcântara do Nascimento"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Thiago Alcântara"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"David Villa Sánchez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"David Villa"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Javier Alejandro Mascherano"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Javier Mascherano"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Andreu Fontàs Prat"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Andreu Fontàs"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Ibrahim Afellay"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Ibrahim Afellay"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Manuel Agudo Durán"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Nolito"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Marc Bartra Aregall"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Marc Bartra"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Adriano Correia Claro"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Adriano"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Martín Montoya Torralbo"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Martín Montoya"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Jonathan dos Santos Ramírez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Jonathan dos Santos"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Francesc Fàbregas i Soler"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Cesc Fàbregas"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Alexis Alejandro Sánchez Sánchez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Alexis Sánchez"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Juan Isaac Cuenca López"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Isaac Cuenca"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Gerard Deulofeu Lázaro"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Gerard Deulofeu"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Cristian Tello"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Cristian Tello"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Sergi Roberto Carnicer"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Sergi Roberto"</span><span class="p">,</span><span class="w">
    </span><span class="n">pass.recipient.name</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Marc Muniesa Martínez"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Marc Muniesa"</span><span class="p">,</span><span class="w">
    </span><span class="kc">TRUE</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="n">pass.recipient.name</span><span class="w">
  </span><span class="p">))</span><span class="w">
</span>

I only changed it for these two variables but you could do it for more
using the scoped variants of mutate() such as mutate_at() or
mutate_if() to change the values of variables that adhere to certain
conditions.

Save Cleaned Data

Now that we’ve got a clean data set it might be a good idea to save it.
I use the here::here() function for setting the path root to the
top-level of the current project directory and then jumping into the
“data” folder. Read this blog post
here
for more info on why it’s useful to do so.

<span class="n">saveRDS</span><span class="p">(</span><span class="n">messi_data_clean</span><span class="p">,</span><span class="w"> </span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">here</span><span class="o">::</span><span class="n">here</span><span class="p">(</span><span class="s2">"data/messi_data_clean.RDS"</span><span class="p">))</span><span class="w">
</span>

To get data for the other data sets it’s a matter of finding and
filtering for the correct “competition_id”. For the Women’s World Cup
data that’ll be 72 and for the Men’s World Cup last year it’ll be
43. The other data cleaning steps are the same.

With a nice clean data set ready, we can move on to reshaping the data
for analysis and plotting!

xG Timeline

Data

To get the data for a single match, in this case an “El Clasico”
match from the 2011/2012 season, we filter() for its “match_id”
number. Our main statistic of interest for the next two plots is going
to be xG in the “shot.statsbomb_xg” variable. If the value for it is
NA we can safely set the value to 0, otherwise we just keep the
value for that row.

We also create a separate data set that sums up the total xG for both
teams and creates a nice label using the {glue} package. The
“team_label” variable will come in handy in the plots. After joining
that data frame in, we also create a “player_label” variable to store
the “player.name” and “shot.statsbomb_xg” values for rows where a
Goal was scored. This variable will also be used as labels in the
plots.

<span class="n">clasico_1112</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">messi_data_clean</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">filter</span><span class="p">(</span><span class="n">match_id</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">69334</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate</span><span class="p">(</span><span class="n">shot.statsbomb_xg</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">if_else</span><span class="p">(</span><span class="nf">is.na</span><span class="p">(</span><span class="n">shot.statsbomb_xg</span><span class="p">),</span><span class="w"> 
                                     </span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="n">shot.statsbomb_xg</span><span class="p">))</span><span class="w">

</span><span class="n">clasico_1112_xg</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><s...

To leave a comment for the author, please follow the link and comment on their blog: R by R(yo).

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)