<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/manipulate-dates-easily-with-lubridate/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.brodrigues.co/blog/2018-12-15-lubridate_africa/"> Econometrics and Free Software</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><a rel="nofollow" href="https://www.youtube.com/watch?v=FTQbiNvZqaY" target="_blank"><br>
<img width="400" title="One of my favourite songs" src="https://i1.wp.com/www.brodrigues.co/blog/2018-12-15-lubridate_africa/img/africa.jpg?w=456" data-recalc-dims="1"></a></p>
</div>
<p></p>
<p>This blog post is an excerpt of my ebook <em>Modern R with the tidyverse</em> that you can read for<br>
free <a rel="nofollow" href="https://b-rodrigues.github.io/modern_R/" target="_blank">here</a>. This is taken from Chapter 5, which presents<br>
the <code>{tidyverse}</code> packages and how to use them to compute descriptive statistics and manipulate data.<br>
In the text below, I scrape a table from Wikipedia, which shows when African countries gained<br>
independence from other countries. Then, using <code>{lubridate}</code> functions I show you how you can<br>
answers questions such as <em>Which countries gained independence before 1960?</em>.</p>
<div>
<h2>Set-up: scraping some data from Wikipedia</h2>
<p><code>{lubridate}</code> is yet another tidyverse package, that makes dealing with dates or duration data<br>
(and intervals) as painless as possible. I do not use every function contained in the package<br>
daily, and as such will only focus on some of the functions. However, if you have to deal with<br>
dates often, you might want to explore the package thoroughly.</p>
<p>Let’s get some data from a Wikipedia table:</p>
<pre><code>library(tidyverse)
library(rvest)</code></pre>
<pre><code>page &lt;- read_html(&quot;https://en.wikipedia.org/wiki/Decolonisation_of_Africa&quot;)

independence &lt;- page %>%
    html_node(&quot;.wikitable&quot;) %>%
    html_table(fill = TRUE)

independence &lt;- independence %>%
    select(-Rank) %>%
    map_df(~str_remove_all(., &quot;\\[.*\\]&quot;)) %>%
    rename(country = `Country[a]`,
           colonial_name = `Colonial name`,
           colonial_power = `Colonial power[b]`,
           independence_date = `Independence date`,
           first_head_of_state = `First head of state[d]`,
           independence_won_through = `Independence won through`)</code></pre>
<p>This dataset was scraped from the following Wikipedia <a rel="nofollow" href="https://en.wikipedia.org/wiki/Decolonisation_of_Africa#Timeline" target="_blank">table</a>.<br>
It shows when African countries gained independence from which colonial powers. In Chapter 11, I<br>
will show you how to scrape Wikipedia pages using R. For now, let’s take a look at the contents<br>
of the dataset:</p>
<pre><code>independence</code></pre>
<pre><code>## # A tibble: 54 x 6
##    country colonial_name colonial_power independence_da… first_head_of_s…
##    </code></pre></div>