<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/analyzing-the-greatest-strikers-in-football-i-getting-data/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.schochastics.net/post/analyzing-the-greatest-strikers-in-football-i-getting-data/"> schochastics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I do not always come up with new ideas for my blog, but rather get inspired by the great work of others.<br>
In this case, it was a <a rel="nofollow" href="https://www.reddit.com/r/soccer/comments/72qw7k/charting_world_footballs_greatest_goal_scorers_oc/" target="_blank">reddit post</a> by u/Cheapo_Sam, who charted world footballs greatest goal scorers in a marvelous way.<br>
According to the post, the data was gathered manually which I thought is too tedious (Ain’t nobody got time for that!).<br>
So I decided to automatize this step.</p>
<p><em>This post exclusively deals with acquisition and cleaning of the data. The next (one or two)<br>
posts will handle the analysis.</em></p>
<pre><code>library(tidyverse)  # for data wrangling
library(rvest)      # for web scraping
library(lubridate)  # for date formats </code></pre>
<p>As a data source, I found <a rel="nofollow" href="https://www.transfermarkt.co.uk/zlatan-ibrahimovic/alletore/spieler/3455/saison//verein/0/liga/0/wettbewerb//pos/0/trainer_id/0/minute/0/torart/0/plus/1" target="_blank">transfermarkt.co.uk</a> to be a good place.<br>
It has very detailed records of goals scored by players, dating back to the 1960’s.<br>
As an example, we will get the data of <a rel="nofollow" href="http://blog.schochastics.net/post/six-degrees-of-zlatan-ibrahimovic/" target="_blank">Zlatan Ibrahimovic</a>.</p>
<pre><code>url &lt;- &quot;https://www.transfermarkt.co.uk/zlatan-ibrahimovic/alletore/spieler/3455/saison//verein/0/liga/0/wettbewerb//pos/0/trainer_id/0/minute/0/torart/0/plus/1&quot;

web &lt;- read_html(url)
doc &lt;- html_table(web, fill = TRUE)</code></pre>
<p>The URL contains a very detailed table of all goals scored by Zlatan.<br>
<code>read_html()</code> reads the html content of the page and <code>html_table()</code> puts all<br>
html tables (<code><table>...&lt;\table>) into a list of data frames. The <code>fill = TRUE</code> is necessary, since<br>
there are some empty cells in columns. Note that there are several tables on the<br>
page. The second one is what we want.
<pre><code>tab &lt;- doc[[2]]
glimpse(tab)</code></pre>
<pre><code>## Observations: 444
## Variables: 18
## $ Competition     </code></pre></table></code>