If you thought of Donald Trump and Narendra Modi when you read the title, yes you were correct. We are going to analyse the twitter of these two these two leaders. They hail from vastly different backgrounds — Donald Trump is the son of a property tycoon, whereas Narendra Modi of a poor tea seller. Even though they come from different backgrounds, they are utterly loved by right wing nationalists, and despised by leftist liberals in their home countries.

I have used R to do this analysis, because of the ease with which it allows us to extract data from Twitter. The data was extracted on 19th May '20. Hence the analysis holds till that date.

The general outline of this article would be this:

Steps for analyse

If you are not interested in the programming part and just want to read the analysis, I suggest you directly jump to section 4.


I. GETTING YOUR TWITTER API ACCESS

To get started, we first need to get Twitter API. This will help us in retrieving tweets — without which we cannot do anything. The process to get Twitter API is fairly easy. You have apply for a developer twitter account via the following link: https://developer.twitter.com/en/apply-for-access.html. After that you will have to fill out the application form, explaining what you wish to analyze and why.

Once twitter checks your profile and gives you the developer account, follow these steps to obtain and use Twitter API access tokens.

Once you have followed the steps, we are good to go. Let’s get started!


II. DATA SET

First make sure you have loaded **rtweet **package. Use **get_token() **function from the package to make sure your credentials are matching.

Now to the first step of getting the tweets. We use the get_timeline function followed by the twitter handle and number of tweets you wish to extract. Note that at one go we can at most extract 3200 tweets only.

#getting recent tweets of Modi and Trump 
modi_tweets <- get_timeline("@narendramodi", n=3200)
trump_tweets <-get_timeline("@realDonaldTrump", n=3200)

The result of the above two lines of code is two gigantic datasets with 90 columns, ranging from variables like text, is_retweet, and retweet_count among others!

The dataset would look something like this:

Modi tweets


III. CLEANING THE DATA

In this step, we will make smaller data sets out of this huge dataset so that it is easier to compare things. If you want you can skip this step.

Any tweet can be broken down into these three categories:

  1. Retweets
  2. Replies
  3. Organic

These are subsets that are mutually exclusive and collectively exhaustive(MECE). We will break our tweets into these categories. To do so, we use basic data frame concepts and **subset **function. We will first remove replies and retweets, which will result in only organic tweets.

# Remove retweets
modi_tweets_organic <- modi_tweets[modi_tweets$is_retweet==FALSE, ]
trump_tweets_organic <- trump_tweets[trump_tweets$is_retweet==FALSE, ]
# Remove replies 
modi_tweets_organic <- subset(modi_tweets_organic, is.na(modi_tweets_organic$reply_to_status_id)) 
trump_tweets_organic <- subset(trump_tweets_organic, is.na(trump_tweets_organic$reply_to_status_id))

Next, we will make a data frame that would contain only retweets and another one that would have only replies. The code would just be the negation of the above lines.

# Keeping only the retweets
modi_retweets <- modi_tweets[modi_tweets$is_retweet==TRUE,]
trump_retweets <- trump_tweets[trump_tweets$is_retweet==TRUE,]
# Keeping only the replies
modi_replies <- subset(modi_tweets, !is.na(modi_tweets$reply_to_status_id))
trump_replies <- subset(trump_tweets, !is.na(trump_tweets$reply_to_status_id))

For some analysis we might need a data frame that contains all the extracted tweets. We will use the **rbind **function to join the original data frame by rows.

tmls <- rbind(modi_tweets, trump_tweets)

Now, for both Modi and Trump we have four data frames(original, organic, retweets, and replies), and one master data frame.

How is data divided

Now, let’s move to the fun part!


IV. ANALYZE

Let’s analyze the data we have extracted and see if we can find any insightful things about the two most popular and powerful right-wing leaders in the world.

Given below is a list of things we will analyze. If you want to read any one section you can skip and read the analysis, but I would appreciate it if you go through the whole thing.

Things that we will analyse!

1. ENGAGEMENT

We will first analyze the engagement of both the accounts, in terms of retweets, likes, and how they interact with their followers.

i. Top 5 liked tweets

We will first find the top five liked tweets of both Modi and Trump, and see if we can find any insight in them. To do so we will consider the variable **favorite_count, **which determines the number of likes for any tweet.

To see the top 5 liked tweets we use the **arrange function from the dplyr **package. This will arrange the tweets in ascending order( lowest to highest favorite count). To get the highest engagement, we arrange the tweets in descending order to do so we use a minus sign “-” before the variable!

First, we do this for Narendra Modi’s tweets:

# top 5 favorite tweets
library(dplyr)
modi_tweets_organic <- modi_tweets_organic %>% 
arrange(-favorite_count)
modi_tweets_organic$text[1:5]

We get the output as:

[1] “शुभं करोति कल्याणमारोग्यं धनसंपदा ।\nशत्रुबुद्धिविनाशाय दीपज्योतिर्नमोऽस्तुते ॥ https://t.co/4DeiMsCN11" 
[2] “सबका साथ + सबका विकास + सबका विश्वास = विजयी भारत\n\nTogether we grow. \n\nTogether we prosper. \n\nTogether we will build a strong and inclusive India. \n\nIndia wins yet again! #VijayiBharat” 
[3] “Irrfan Khan’s demise is a loss to the world of cinema and theatre. He will be remembered for his versatile performances across different mediums. My thoughts are with his family, friends and admirers. May his soul rest in peace.”
[4] “रामनवमी के पावन अवसर पर समस्त देशवासियों को हार्दिक शुभकामनाएं। जय श्रीराम!” 
[5] “हनुमान जयंती के पावन अवसर पर देशवासियों को हार्दिक शुभकामनाएं। भक्ति, शक्ति, समर्पण और अनुशासन के प्रतीक पवनपुत्र का जीवन हमें हर संकट का सामना करने और उससे पार पाने की प्रेरणा देता है।”

The most-liked tweet of Narendra Modi is related to his 9pm9minute campaign, the tweet is a picture of him lighting the lamp.

Narendra Modi’s most liked tweet

We also notice that PM Modi’s tweet([3]) about the beloved Indian actor Irrfan Khan’s demise. One can also notice that veteran actor Rishi Kapoor also passed away the next day but PM Modi’s tweet about him is not in this list.

Another point worth noticing is that tweets about festivals([4],[5]) also made it to the list.

Now we will do the same thing for President Donald Trump:

trump_tweets_organic <- trump_tweets_organic %>%
                        arrange(-favorite_count)
trump_tweets_organic$text[1:5]

We get the output as:

[1] “THE UNITED STATES LOVES ITALY! https://t.co/DESUJ4QM41" 
[2] “Happy Birthday to Melania, our great First Lady!” 
[3] “The world is at war with a hidden enemy. WE WILL WIN!” 
[4] “HAPPY EASTER!” 
[5] “I am a great friend and admirer of the Queen &amp; the United Kingdom. It was reported that Harry and Meghan, who left the Kingdom, would reside permanently in Canada. Now they have left Canada for the U.S. however, the U.S. will not pay for their security protection. They must pay!”

Donald Trump’s most liked tweet

The most-liked tweet of Trump was about a clip to encourage Italy. Italy, after China is one of the most affected countries by the Novel Coronavirus pandemic. This clip of Frecce Tricolori, the Italian Air Force display team, was used to boost the morale of people in these tough times. Another tweet([3]) about coronavirus made it to most-liked tweets.

His wishes to FLOTUS also comes among these tweets, as is his greetings for Easter.

We notice the commonalities in these two lists. People have liked the words of encouragement from both the leaders the most. Most liked tweets featured the coronavirus, something with which the whole world is struggling a lot. Tweets about festivals are also liked a lot.

#donald-trump #analysis #narendra-modi #twitter #data-science #data analysis

TWITTER ANALYSIS OF THE TWO MOST POPULAR RIGHT-WING POLITICIANS
1.10 GEEK