Visualizing the Copa América: Historical Records, Squad Profiles, and Player Profiles with xG statistics!

[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.

Another summer and another edition of the Copa América! Along with the
Africa Cup of Nations, Nations League finals, the Women’s World Cup,
Under-21 European Championship AND the Gold Cup this is yet another
soccer-filled season after last year’s World Cup and the Asian Cup
earlier this year (I also did a blog post on these last two tournaments
which you can see here (World
Cup)
and here
(Asian Cup)
).
There is so much football going on at once even I can’t keep up,
especially with the time difference! To not redo all the previous
visualizations with Copa América data I tried to find new sources of
data and other forms of visualizations to give some insight into the
players and teams competing to be the champion of South America. You can
find all the code I used in this blogpost here and you can also find
other soccer related data viz in my
soccer_ggplot Github repo.

The sections will go from a very macro-level view of the historical
records
of the tournament, to the squads competing, the teams’
match record in the Copa América, and finally to a micro-level view
of various attacking players using xG statistics.

¡Vámonos!

Packages

<span class="n">library</span><span class="p">(</span><span class="n">dplyr</span><span class="p">)</span><span class="w">        </span><span class="c1">## data wrangling</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">tidyr</span><span class="p">)</span><span class="w">        </span><span class="c1">## data wrangling</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">purrr</span><span class="p">)</span><span class="w">        </span><span class="c1">## data wrangling and iteration</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">stringr</span><span class="p">)</span><span class="w">      </span><span class="c1">## data wrangling</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">rvest</span><span class="p">)</span><span class="w">        </span><span class="c1">## webscraping</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">polite</span><span class="p">)</span><span class="w">       </span><span class="c1">## webscraping (Github only pkg)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">ggplot2</span><span class="p">)</span><span class="w">      </span><span class="c1">## plotting</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">scales</span><span class="p">)</span><span class="w">       </span><span class="c1">## plotting scales</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">ggimage</span><span class="p">)</span><span class="w">      </span><span class="c1">## images for flags</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">ggforce</span><span class="p">)</span><span class="w">      </span><span class="c1">## plotting text labels</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">cowplot</span><span class="p">)</span><span class="w">      </span><span class="c1">## plotting grid</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">glue</span><span class="p">)</span><span class="w">         </span><span class="c1">## text</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">ggrepel</span><span class="p">)</span><span class="w">      </span><span class="c1">## plotting text labels</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">magick</span><span class="p">)</span><span class="w">       </span><span class="c1">## plotting</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">kable</span><span class="p">)</span><span class="w">        </span><span class="c1">## tables</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">ggtextures</span><span class="p">)</span><span class="w">   </span><span class="c1">## soccer ball emoji as geom_col()</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">extrafont</span><span class="p">)</span><span class="w">    </span><span class="c1">## fonts: Roboto Condensed</span><span class="w">

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

theme_copaAmerica

I wanted to have all the plots in this blogpost to have a consistent
color theme. As the tournament is going to be held in Brazil, I went
with a color theme based on its flag with blue, yellow, and green being
the primary colors.

<span class="n">theme_copaAmerica</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="w">
  </span><span class="n">title.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">24</span><span class="p">,</span><span class="w">
  </span><span class="n">subtitle.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">14</span><span class="p">,</span><span class="w">
  </span><span class="n">caption.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">8</span><span class="p">,</span><span class="w">
  </span><span class="n">axis.text.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">14</span><span class="p">,</span><span class="w">
  </span><span class="n">axis.text.x.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">12</span><span class="p">,</span><span class="w">
  </span><span class="n">axis.text.y.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">12</span><span class="p">,</span><span class="w">
  </span><span class="n">axis.title.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">16</span><span class="p">,</span><span class="w">
  </span><span class="n">strip.text.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">18</span><span class="p">,</span><span class="w">
  </span><span class="n">panel.grid.major.x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_line</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0.5</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"white"</span><span class="p">),</span><span class="w">
  </span><span class="n">panel.grid.major.y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_line</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0.5</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"white"</span><span class="p">),</span><span class="w">
  </span><span class="n">panel.grid.minor.x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
  </span><span class="n">panel.grid.minor.y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
  </span><span class="n">axis.ticks</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_line</span><span class="p">(</span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"white"</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w">
  </span><span class="c1">## Theme:</span><span class="w">
  </span><span class="n">theme</span><span class="p">(</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Roboto Condensed"</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"white"</span><span class="p">),</span><span class="w">
        </span><span class="n">plot.title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Roboto Condensed"</span><span class="p">,</span><span class="w"> </span><span class="n">face</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"bold"</span><span class="p">,</span><span class="w"> 
                                  </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">title.size</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"yellow"</span><span class="p">),</span><span class="w">
        </span><span class="n">plot.subtitle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">subtitle.size</span><span class="p">),</span><span class="w">
        </span><span class="n">plot.caption</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">caption.size</span><span class="p">),</span><span class="w">
        </span><span class="n">panel.background</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_rect</span><span class="p">(</span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#009b3a"</span><span class="p">),</span><span class="w">
        </span><span class="n">plot.background</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_rect</span><span class="p">(</span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#002776"</span><span class="p">),</span><span class="w">
        </span><span class="n">axis.text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">axis.text.size</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"white"</span><span class="p">),</span><span class="w">
        </span><span class="n">axis.text.x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">axis.text.x.size</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"white"</span><span class="p">),</span><span class="w">
        </span><span class="n">axis.text.y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">axis.text.y.size</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"white"</span><span class="p">),</span><span class="w">
        </span><span class="n">axis.title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">axis.title.size</span><span class="p">),</span><span class="w">
        </span><span class="n">axis.line.x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
        </span><span class="n">axis.line.y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
        </span><span class="n">panel.grid.major.x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">panel.grid.major.x</span><span class="p">,</span><span class="w">
        </span><span class="n">panel.grid.major.y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">panel.grid.major.y</span><span class="p">,</span><span class="w">
        </span><span class="n">panel.grid.minor.x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">panel.grid.minor.x</span><span class="p">,</span><span class="w">
        </span><span class="n">panel.grid.minor.y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">panel.grid.minor.y</span><span class="p">,</span><span class="w">
        </span><span class="n">strip.text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"yellow"</span><span class="p">,</span><span class="w"> </span><span class="n">face</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"bold"</span><span class="p">,</span><span class="w"> 
                                  </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">strip.text.size</span><span class="p">,</span><span class="w"> 
                                  </span><span class="n">margin</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">margin</span><span class="p">(</span><span class="m">4.4</span><span class="p">,</span><span class="w"> </span><span class="m">4.4</span><span class="p">,</span><span class="w"> </span><span class="m">4.4</span><span class="p">,</span><span class="w"> </span><span class="m">4.4</span><span class="p">)),</span><span class="w">
        </span><span class="n">strip.background</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
        </span><span class="n">axis.ticks</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">axis.ticks</span><span class="w">
        </span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span>

Top Goal Scorers // Goleadores

For this plot I took the stats from the Spanish version of the Wikipedia
page as it had more content. I used purrr::flatten_df() to squish the
list output into a dataframe then set the names of each column using
purrr::set_names().

<span class="n">url</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"https://es.wikipedia.org/wiki/Anexo:Estad%C3%ADsticas_de_la_Copa_Am%C3%A9rica"</span><span class="w">

</span><span class="n">session</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">bow</span><span class="p">(</span><span class="n">url</span><span class="p">)</span><span class="w">

</span><span class="n">copa_top_scorers</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">scrape</span><span class="p">(</span><span class="n">session</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">".mw-parser-output > table:nth-child(95)"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">html_table</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">flatten_df</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">set_names</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="s2">"player"</span><span class="p">,</span><span class="w"> </span><span class="s2">"country"</span><span class="p">,</span><span class="w"> </span><span class="s2">"goals"</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">image</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"https://www.emoji.co.uk/files/microsoft-emojis/activity-windows10/8356-soccer-ball.png"</span><span class="p">)</span><span class="w">
</span>
<span class="n">glimpse</span><span class="p">(</span><span class="n">copa_top_scorers</span><span class="p">)</span><span class="w">
</span>
## Observations: 22
## Variables: 4
## $ player  <chr> "Norberto Méndez", "Zizinho", "Lolo Fernández", "Sever...
## $ country <chr> "ARG Argentina", "BRA Brasil", "PER Perú", "URU Urugua...
## $ goals   <int> 17, 17, 15, 15, 13, 13, 13, 13, 13, 12, 12, 11, 11, 11...
## $ image   <chr> "https://www.emoji.co.uk/files/microsoft-emojis/activi...

Like in the Asian Cup blogpost I use Claus
Wilke
’s
ggtextures package to use
soccer ball emoji as the column image in the plot.

<span class="n">copa_goleadores_raw_plot</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">copa_top_scorers</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">head</span><span class="p">(</span><span class="m">5</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">ggplot</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">reorder</span><span class="p">(</span><span class="n">player</span><span class="p">,</span><span class="w"> </span><span class="n">goals</span><span class="p">),</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">goals</span><span class="p">,</span><span class="w">
             </span><span class="n">image</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">image</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_isotype_col</span><span class="p">(</span><span class="n">img_width</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">grid</span><span class="o">::</span><span class="n">unit</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="s2">"native"</span><span class="p">),</span><span class="w"> </span><span class="n">img_height</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NULL</span><span class="p">,</span><span class="w">
    </span><span class="n">ncol</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NA</span><span class="p">,</span><span class="w"> </span><span class="n">nrow</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="n">hjust</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="n">vjust</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0.5</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_text</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">goals</span><span class="p">,</span><span class="w"> </span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Roboto Condensed"</span><span class="p">,</span><span class="w"> </span><span class="n">fontface</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"bold"</span><span class="p">),</span><span class="w"> 
            </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">7.5</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"yellow"</span><span class="p">,</span><span class="w">
            </span><span class="n">nudge_y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0.5</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">coord_flip</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_y_continuous</span><span class="p">(</span><span class="n">breaks</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">6</span><span class="p">,</span><span class="w"> </span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="m">10</span><span class="p">,</span><span class="w"> </span><span class="m">12</span><span class="p">,</span><span class="w"> </span><span class="m">14</span><span class="p">,</span><span class="w"> </span><span class="m">16</span><span class="p">,</span><span class="w"> </span><span class="m">18</span><span class="p">),</span><span class="w">
                     </span><span class="n">expand</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">0</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">limits</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="m">19</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">labs</span><span class="p">(</span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Top Scorers of the Copa América"</span><span class="p">,</span><span class="w">
       </span><span class="n">subtitle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">glue</span><span class="p">(</span><span class="s2">"
                       Most goals in a single tournament: 9 
                       Humberto Maschio (Argentina), Javier Ambrois (Uruguay), Jair (Brazil)"</span><span class="p">),</span><span class="w">
       </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Number of Goals"</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NULL</span><span class="p">,</span><span class="w">
       </span><span class="n">caption</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">glue</span><span class="p">(</span><span class="s2">"
                      Source: Wikipedia
                      By @R_by_Ryo"</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme_copaAmerica</span><span class="p">(</span><span class="n">title.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">26</span><span class="p">,</span><span class="w">
                    </span><span class="n">subtitle.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">16</span><span class="p">,</span><span class="w">
                    </span><span class="n">caption.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">12</span><span class="p">,</span><span class="w">
                    </span><span class="n">axis.text.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">18</span><span class="p">,</span><span class="w">
                    </span><span class="n">axis.title.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">18</span><span class="p">,</span><span class="w">
                    </span><span class="n">panel.grid.major.y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
                    </span><span class="n">axis.ticks</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">())</span><span class="w">

</span><span class="c1">## Add flags to y-axis:</span><span class="w">
</span><span class="n">axis_image</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">axis_canvas</span><span class="p">(</span><span class="n">copa_goleadores_raw_plot</span><span class="p">,</span><span class="w"> </span><span class="n">axis</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'y'</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> 
  </span><span class="n">draw_image</span><span class="p">(</span><span class="s2">"https://upload.wikimedia.org/wikipedia/en/0/05/Flag_of_Brazil.svg"</span><span class="p">,</span><span class="w"> 
             </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">16.5</span><span class="p">,</span><span class="w"> </span><span class="n">scale</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.8</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">draw_image</span><span class="p">(</span><span class="s2">"https://upload.wikimedia.org/wikipedia/commons/1/1a/Flag_of_Argentina.svg"</span><span class="p">,</span><span class="w"> 
             </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">12.5</span><span class="p">,</span><span class="w"> </span><span class="n">scale</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.8</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">draw_image</span><span class="p">(</span><span class="s2">"https://upload.wikimedia.org/wikipedia/commons/f/fe/Flag_of_Uruguay.svg"</span><span class="p">,</span><span class="w"> 
             </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="n">scale</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.8</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">draw_image</span><span class="p">(</span><span class="s2">"https://upload.wikimedia.org/wikipedia/commons/d/df/Flag_of_Peru_%28state%29.svg"</span><span class="p">,</span><span class="w"> 
             </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5.25</span><span class="p">,</span><span class="w"> </span><span class="n">scale</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.8</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">draw_image</span><span class="p">(</span><span class="s2">"https://upload.wikimedia.org/wikipedia/en/0/05/Flag_of_Brazil.svg"</span><span class="p">,</span><span class="w"> 
             </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.5</span><span class="p">,</span><span class="w"> </span><span class="n">scale</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.8</span><span class="p">)</span><span class="w">

</span><span class="n">copa_goleadores_plot</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ggdraw</span><span class="p">(</span><span class="n">insert_yaxis_grob</span><span class="p">(</span><span class="n">copa_goleadores_raw_plot</span><span class="p">,</span><span class="w"> </span><span class="n">axis_image</span><span class="p">,</span><span class="w"> </span><span class="n">position</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"left"</span><span class="p">))</span><span class="w">
</span><span class="n">copa_goleadores_plot</span><span class="w">
</span>

Most of these players aren’t ones you might recognize. The Copa América
used to be held a lot more regularly (and sometimes erratically) until
this century so players had a lot more opportunities to score goals. All
five of the players you see here played in the 1930s-1950s when there
was a tournament every one or two years. Out of currently active
players, Peruvian legend Paolo Guerrero has 11 goals along with Eduardo
Vargas (from Chile) with 10. (Edit: after the Chile – Japan game, Vargas is on
12…) Another player you might recognize that was actually tied with
Ademir for 5th place, along with three other players, was Gabriel
Batistuta (“Batigol”).

Winners of the Copa América

After grabbing the data from the Wikipedia page I used a variety of
functions to clean and reshape the dataset like tidyr::separate() to
split the number of occurences and the year.

<span class="n">url</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"https://es.wikipedia.org/wiki/Anexo:Estad%C3%ADsticas_de_la_Copa_Am%C3%A9rica"</span><span class="w">

</span><span class="n">session</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">bow</span><span class="p">(</span><span class="n">url</span><span class="p">)</span><span class="w">

</span><span class="n">copa_campeones</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">scrape</span><span class="p">(</span><span class="n">session</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">".mw-parser-output > table:nth-child(10)"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">html_table</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">flatten_df</span><span class="p">()</span><span class="w">

</span><span class="n">copa_campeones_limpia</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">copa_campeones</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">janitor</span><span class="o">::</span><span class="n">clean_names</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">slice</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">8</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">select</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">4</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">set_names</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="s2">"team"</span><span class="p">,</span><span class="w"> </span><span class="s2">"winners"</span><span class="p">,</span><span class="w"> </span><span class="s2">"runners_up"</span><span class="p">,</span><span class="w"> </span><span class="s2">"third_place"</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">separate</span><span class="p">(</span><span class="n">winners</span><span class="p">,</span><span class="w"> </span><span class="n">into</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"Champions"</span><span class="p">,</span><span class="w"> </span><span class="s2">"first_place_year"</span><span class="p">),</span><span class="w"> 
           </span><span class="n">sep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">" "</span><span class="p">,</span><span class="w"> </span><span class="n">extra</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"merge"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">separate</span><span class="p">(</span><span class="n">runners_up</span><span class="p">,</span><span class="w"> </span><span class="n">into</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"Runners-up"</span><span class="p">,</span><span class="w"> </span><span class="s2">"second_place_year"</span><span class="p">),</span><span class="w"> 
           </span><span class="n">sep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">" "</span><span class="p">,</span><span class="w"> </span><span class="n">extra</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"merge"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">separate</span><span class="p">(</span><span class="n">third_place</span><span class="p">,</span><span class="w"> </span><span class="n">into</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"Third Place"</span><span class="p">,</span><span class="w"> </span><span class="s2">"third_place_year"</span><span class="p">),</span><span class="w"> 
           </span><span class="n">sep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">" "</span><span class="p">,</span><span class="w"> </span><span class="n">extra</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"merge"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate_all</span><span class="p">(</span><span class="nf">list</span><span class="p">(</span><span class="o">~</span><span class="n">str_replace_all</span><span class="p">(</span><span class="n">.</span><span class="p">,</span><span class="w"> </span><span class="s2">"–"</span><span class="p">,</span><span class="w"> </span><span class="s2">"0"</span><span class="p">)))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate_at</span><span class="p">(</span><span class="n">vars</span><span class="p">(</span><span class="n">contains</span><span class="p">(</span><span class="s2">"num"</span><span class="p">)),</span><span class="w"> </span><span class="n">funs</span><span class="p">(</span><span class="n">as.numeric</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">gather</span><span class="p">(</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"key"</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"value"</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="n">team</span><span class="p">,</span><span class="w"> 
         </span><span class="o">-</span><span class="n">first_place_year</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="n">second_place_year</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="n">third_place_year</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">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">as.factor</span><span class="p">(</span><span class="n">key</span><span class="p">),</span><span class="w">
         </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(</span><span class="n">value</span><span class="p">),</span><span class="w">
         </span><span class="n">team</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">team</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">str_replace</span><span class="p">(</span><span class="n">.</span><span class="p">,</span><span class="w"> </span><span class="s2">"[A-Z]{3}"</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">str_trim</span><span class="p">(</span><span class="n">.</span><span class="p">),</span><span class="w">
         </span><span class="n">team</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="n">team</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"Brasil"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Brazil"</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">team</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">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">forcats</span><span class="o">::</span><span class="n">fct_relevel</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="w"> 
                                    </span><span class="s2">"Champions"</span><span class="p">,</span><span class="w">
                                    </span><span class="s2">"Runners-up"</span><span class="p">,</span><span class="w">
                                    </span><span class="s2">"Third Place"</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">arrange</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">desc</span><span class="p">(</span><span class="n">value</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">team</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">forcats</span><span class="o">::</span><span class="n">as_factor</span><span class="p">(</span><span class="n">team</span><span class="p">),</span><span class="w">
         </span><span class="n">order</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">row_number</span><span class="p">())</span><span class="w">
</span>

I also wanted to add flags to this plot but
cowplot::insert_yaxis_grob() is unfortunately not compatible with
facets. I used stringr::str_wrap() to format the subtitle nicely while
I used glue::glue() to avoid having the use ‘\n’ to create a new line
for the caption.

<span class="n">copa_ganadores_plot</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">copa_campeones_limpia</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">ggplot</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">value</span><span class="p">,</span><span class="w"> </span><span class="n">forcats</span><span class="o">::</span><span class="n">fct_rev</span><span class="p">(</span><span class="n">team</span><span class="p">),</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">key</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_point</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">10</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">        </span><span class="c1"># 10</span><span class="w">
  </span><span class="n">geom_text</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">value</span><span class="p">),</span><span class="w"> 
            </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"black"</span><span class="p">,</span><span class="w">    </span><span class="c1"># 5 </span><span class="w">
            </span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Roboto Condensed"</span><span class="p">,</span><span class="w"> </span><span class="n">fontface</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"bold"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_color_manual</span><span class="p">(</span><span class="n">values</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"Champions"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#FFCC33"</span><span class="p">,</span><span class="w">
                                </span><span class="s2">"Runners-up"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#999999"</span><span class="p">,</span><span class="w">
                                </span><span class="s2">"Third Place"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#CC6600"</span><span class="p">),</span><span class="w">
                     </span><span class="n">guide</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_x_continuous</span><span class="p">(</span><span class="n">breaks</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="m">10</span><span class="p">,</span><span class="w"> </span><span class="m">15</span><span class="p">),</span><span class="w">
                     </span><span class="n">labels</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="m">10</span><span class="p">,</span><span class="w"> </span><span class="m">15</span><span class="p">),</span><span class="w">
                     </span><span class="n">limits</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">-1</span><span class="p">,</span><span class="w"> </span><span class="m">16</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">labs</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Number of Occurrence"</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NULL</span><span class="p">,</span><span class="w">
       </span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Most Successful Teams of the Copa América!"</span><span class="p">,</span><span class="w">
       </span><span class="n">subtitle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">str_wrap</span><span class="p">(</span><span class="s2">"Ordered by number of Copa América(s) won. Argentina missed the chance to leapfrog Uruguay after consecutive final losses in the previous two tournaments!"</span><span class="p">,</span><span class="w"> </span><span class="n">width</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">80</span><span class="p">),</span><span class="w">
       </span><span class="n">caption</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">glue</span><span class="p">(</span><span class="s2">"
                      Source: Wikipedia
                      By @R_by_Ryo"</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">facet_wrap</span><span class="p">(</span><span class="o">~</span><span class="n">key</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme_copaAmerica</span><span class="p">(</span><span class="n">subtitle.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">14</span><span class="p">,</span><span class="w"> 
                    </span><span class="n">caption.size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">10</span><span class="p">)</span><span class="w">

</span><span class="n">copa_ganadores_plot</span><span class="w">
</span>

What’s surprising to note is that Pele never won a Copa América with
Brazil, although he did get Best Player and Top Scorer in the 1959
edition of the tournament. Even more bizarrely Diego Maradona has never
won it either! He didn’t play in either of the 1991 and 1993 editions
where Argentina won their 13th and 14th Copas.

Copa América Squad Profiles

We just looked at what happened in the past but who are the players
competing in the tournament this year? To take a quick look I
web-scraped the squads of each of the competing teams from Wikipedia.

I created a list of the xpaths for each of squads and using
purrr::map() I grabbed the data for each participating country. After
I got some meta-information about the country name and the group I
created a list-column that stores the squad data as a dataframe in its
own column. To explode this out I used tidyr::unnest() to reshape the
entire dataframe to have one row with all the data for each player in
every squad.

To get a clean dataset I use some stringr::str_*() functions to
properly format the character strings such as the player positions,
ages, date of births.

<span class="n">squads_df_clean</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">squads_df_raw</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">janitor</span><span class="o">::</span><span class="n">clean_names</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">select</span><span class="p">(</span><span class="o">-</span><span class="n">delete</span><span class="p">,</span><span class="w"> </span><span class="n">squad_num</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">no</span><span class="p">,</span><span class="w"> 
         </span><span class="n">position</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pos</span><span class="p">,</span><span class="w"> </span><span class="n">birth_age</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">date_of_birth_age</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">position</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">position</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">str_replace_all</span><span class="p">(</span><span class="n">.</span><span class="p">,</span><span class="w"> </span><span class="s2">"[1-9]"</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">),</span><span class="w">
         </span><span class="n">birth_age</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">birth_age</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">str_extract_all</span><span class="p">(</span><span class="n">.</span><span class="p">,</span><span class="w"> </span><span class="n">pattern</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"\\([^()]+\\)"</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w">   </span><span class="n">unnest</span><span class="p">(</span><span class="n">birth_age</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">group_by</span><span class="p">(</span><span class="n">player</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">colnum</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">seq_along</span><span class="p">(</span><span class="n">player</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">spread</span><span class="p">(</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">colnum</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">birth_age</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">ungroup</span><span class="p">()</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">everything</span><span class="p">(),</span><span class="w"> </span><span class="n">dob</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">`1`</span><span class="p">,</span><span class="w"> </span><span class="n">age</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">`2`</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">dob</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dob</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">str_replace_all</span><span class="p">(</span><span class="n">.</span><span class="p">,</span><span class="w"> </span><span class="s2">"[()]"</span><span class="p">,</span><span class="w"> </span><span class="s2">""</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">lubridate</span><span class="o">::</span><span class="n">as_date</span><span class="p">(),</span><span class="w">
         </span><span class="n">age</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">age</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">str_extract</span><span class="p">(</span><span class="n">.</span><span class="p">,</span&g...

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)