<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/which-world-leaders-are-twitter-bots/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.jumpingrivers.com/posts/2018/rtweet_leaders/"> R on The Jumping Rivers Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<ul>
<li><a rel="nofollow" href="https://blog.jumpingrivers.com/posts/2018/rtweet_leaders/#set-up" target="_blank">Set-up</a></li>
<li><a rel="nofollow" href="https://blog.jumpingrivers.com/posts/2018/rtweet_leaders/#getting-the-tweets" target="_blank">Getting the tweets</a></li>
<li><a rel="nofollow" href="https://blog.jumpingrivers.com/posts/2018/rtweet_leaders/#are-world-leaders-actually-bots" target="_blank">Are world leaders actually bots?</a></li>
</ul>
</div>
<div>
<h2>Set-up</h2>
<p>Given that I do quite like twitter, I thought it would be a good idea to right about R’s interface to the twitter API; <strong>rtweet</strong>. As usual, we can grab the package in the usual way. We’re also going to need the <strong>tidyverse</strong> for the analysis, <strong>rvest</strong> for some initial webscraping of twitter names, <strong>lubridate</strong> for some date manipulation and <strong>stringr</strong> for some minor text mining.</p>
<pre><code>install.packages(c(&quot;rtweet&quot;, &quot;tidyverse&quot;, &quot;rvest&quot;, &quot;lubridate&quot;))</code></pre>
<pre><code>library(&quot;rtweet&quot;)
library(&quot;tidyverse&quot;)
library(&quot;rvest&quot;)
library(&quot;lubridate&quot;)</code></pre>
</div>
<div>
<h2>Getting the tweets</h2>
<p>So, I could just write the names of twitter’s 10 most followed world leaders, but what would be the fun in that? We’re going to scrape them from <a rel="nofollow" href="https://twiplomacy.com/ranking/the-50-most-followed-world-leaders-in-2017/" target="_blank">twiplomacy</a> using <strong>rvest</strong> and a chrome extension called <a rel="nofollow" href="https://blog.jumpingrivers.com/posts/2018/rtweet_leaders/" target="_blank">selector gadget</a>:</p>
<pre><code>world_leaders = read_html(&quot;https://twiplomacy.com/ranking/the-50-most-followed-world-leaders-in-2017/&quot;)
lead_r = world_leaders %>% 
  html_nodes(&quot;.ranking-entry .ranking-user-name&quot;) %>%
  html_text() %>%
  str_replace_all(&quot;\\t|\\n|@&quot;, &quot;&quot;)
head(lead_r)</code></pre>
<pre><code>## [1] &quot;realdonaldtrump&quot; &quot;pontifex&quot;        &quot;narendramodi&quot;    &quot;pmoindia&quot;       
## [5] &quot;potus&quot;           &quot;whitehouse&quot;</code></pre>
<p>The string inside <code>html_nodes()</code> is gathered using selector gadget. See this <a rel="nofollow" href="http://blog.rstudio.com/2014/11/24/rvest-easy-web-scraping-with-r/" target="_blank">great tutorial</a> on <strong>rvest</strong> and for more on selector gadget read <code>vignette(&quot;selectorgadget&quot;)</code>. Tabs (<code>\t</code>) and linebreaks (<code>\n</code>) are removed with <code>str_replace_all()</code> from the <strong>stringr</strong> package.</p>
<p>Now we can collect the twitter data using <strong>rtweet</strong>. We can use the function <code>lookup_users()</code> to grab basic user info such as number of tweets, friends, favourites and followers. Obviously analysing all 50 leaders at once would be a pain. So we’re only going to take the top 10 (<em><strong>WARNING</strong></em>: this could take a while)</p>
<pre><code>lead_r_info = lookup_users(lead_r[1:10])
lead_r_info</code></pre>
<pre><code>## # A tibble: 10 x 20
##    user_id  name   screen_name  location description       url   protected
##    </code></pre></div>