<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/player-data-for-the-2018-fifa-world-cup/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2018/06/14/player-data-for-the-2018-fifa-world-cup/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The World Cup starts today! The tournament which runs from June 14 through July 15 is probably the most popular sporting event in the world. if you are a soccer fan, you know that learning about the players and their teams and talking about it all with your friends greatly enhances the experience. In this post, I will show you how to gather and explore data for the 736 players from the 32 teams at the 2018 FIFA World Cup. Have fun and enjoy the games. I will be watching with you.</p>
<div>
<h2>Download Player Data</h2>
<div>
<h3>Official PDF</h3>
<p>FIFA has made several official player lists available, conveniently changing the format each time. For this exercise, I use the one from early June. The <a rel="nofollow" href="https://cran.r-project.org/package=tabulizer" target="_blank">tabulizer package</a> makes extracting information from tables included in a PDF document relatively easy. (The other (later) version of the official PDF is <a rel="nofollow" href="https://github.com/davidkane9/wc18/raw/master/fifa_player_list_2.pdf" target="_blank">here</a>. Strangely, the weight variable has been dropped.)</p>
<pre><code>suppressMessages(library(tidyverse))
library(stringr)
suppressMessages(library(lubridate))
suppressMessages(library(cowplot))

# Note that I set warnings to FALSE because of some annoying (and intermittent)
# issues with RJavaTools.

library(tabulizer)
url &lt;- &quot;https://github.com/davidkane9/wc18/raw/master/fifa_player_list_1.pdf&quot;
out &lt;- extract_tables(url, output = &quot;data.frame&quot;)</code></pre>
<p>We now have a 32 element list, each item a data frame of information about the 23 players on each team. Let’s combine this information into a single tidy tibble.</p>
<pre><code># Note how bind_rows() makes it very easy to combine a list of compatible
# dataframes.

pdf_data &lt;- bind_rows(out) %>% 
  as_tibble() %>% 
  
  # Make the variable names more tidy-like.
  
  rename(team = Team,
         number = X.,
         position = Pos.,
         name = FIFA.Popular.Name,
         birth_date = Birth.Date,
         shirt_name = Shirt.Name,
         club = Club,
         height = Height,
         weight = Weight) %>% 
  
  # Country names are contentious issues. I modify two names because I will
  # later need to merge this tibble with data from Wikipedia, which uses
  # different names.
  
  mutate(team = case_when(
         team == &quot;Korea Republic&quot; ~ &quot;South Korea&quot;,
         team == &quot;IR Iran&quot; ~ &quot;Iran&quot;,
         TRUE ~ team)) %>% 
  
  # league and club should be separate variables. We also want birth_date to be
  # a date and to have an age variable already calculated.
  
  mutate(birth_date = dmy(birth_date),
         league = str_sub(club, -4, -2),
         club = str_sub(club, end = -7),
         age = interval(birth_date, &quot;2018-06-14&quot;) / years(1))</code></pre>
<p>Here is a sample of the data:</p>
<pre><code>## # A tibble: 10 x 10
##    team      number position birth_date shirt_name    club     height weight league   age
##    </code></pre></div></div>