Analyzing #WhenTrumpIsOutOfOffice tweets
By: Feng Lim
Tags: Naturallanguageprocessing, Data Analysis, Politics, Donald Trump, Data Science

As we get closer to the U.S.’s next presidential election, I wanted to know what people are thinking of the nominees. Will, the current President, continue his stay at the White House, or will we see a new U.S. President with a less angry Twitter rant?

Getting the dataset

I used the R package rtweet to download tweets with the hashtag #WhenTrumpIsOutOfOffice tweeted in March 2020. As a result, I was able to find more than 6000 tweets with the hashtag.

library(rtweet) # create token named "twitter_token"

twitter_token <- create_token(

app = appname,

consumer_key = consumer_key,

consumer_secret = consumer_secret,

access_token = access_token,

access_secret = access_secret) #download tweets into csv files

tweets <- search_tweets(

"#WhenTrumpIsOutOfOffice", n = 18000, include_rts = FALSE) df <- apply(tweets,2,as.character)

write.csv(df,"csv file path" ) #read the csv file

text <- read.csv("csv file path", stringsAsFactors = FALSE)

Cleaning the dataset

Like any other Data Science project, data cleaning is essential. In this article, I removed URLs, hashtags, and common stop words from the tweet texts.

#data pre-processing

remove_reg <- "&|<|>"

text <- text %>%

mutate(text = str_remove_all(text, remove_reg)) %>%

mutate(text = tolower(text)) %>%

mutate(text = str_replace_all(text, regex("@\\w+"),"" )) %>%

mutate(text = str_replace_all(text, regex("http\\w+"),"" )) %>%

mutate(text = str_replace_all(text, regex("://t.co/\\w+"),"" ))%>%

mutate(text = str_replace_all(text, regex("<\\w+"),"" )) %>%

mutate(text = str_replace_all(text, regex("/+[a-zA-Z0-9<>]+"),"")) %>%

mutate(text = str_replace_all(text, regex("fa[a-zA-Z0-9+<>]+"),"" )) %>%

mutate(text = str_replace_all(text, regex("#[a-zA-Z0-9<>]+"),"" ))

Next, I used the R package udpipe to annotate the tweets we downloaded.

library(udpipe) #download and load the pre-trained models

udmodel <- udpipe_download_model(language = "english")

udmodel <- udpipe_load_model(file = udmodel$file_model) #annotate the data frame with uipipe model

tidy_text <- udpipe_annotate(udmodel, x = text$text)

tidy_text <- as.data.frame(tidy_text)

After annotating the dataset, we can remove stop words from our dataset.

#Remove stop words

my_stop_words <- tibble(

word = c("#whentrumpisoutofoffice", " my_stop_words @realdonaldtrump ", "trump","ill")) #Prepare stop words tibble

all_stop_words <- stop_words %>%

bind_rows(my_stop_words) tidy_tweets <- text %>%

anti_join(all_stop_words, by = "word") #To check how many words removed after stop word anti join, deleted

tibble(

total_words = nrow(text),

after_cleanup = nrow(tidy_tweets))

Word count before and after data cleaning

Data analysis

In this article, you will find four different text mining techniques applied to the tweets:

1. Most frequent words in the tweets

2. Keyword extraction

3. Sentiment analysis

4. Word association network graphs

Most retweeted text

Two tweets have more than a thousand retweets:

#WhenTrumpIsOutOfOffice these lawless, corrupt, unscrupulous, morons will be gone with him. — 1216 retweets

I’m automatically attracted to voting bad politicians out of office. I just start voting; it’s like a magnet. Just vote. I don’t even wait. And when it’s a democracy, they let you do it. #WhenTrumpIsOutOfOffice — 1101 retweets

Uni-gram and Bi-gram Word frequencies

One of the popular text mining techniques is to find out how often a word appears in one or many documents. In this article, we will look at the word frequency for uni-grams (1-word) and bi-grams (2-word). While uni-grams give us a glance of the most used word in the document, bi-grams usually give readers better insights.

From the bar charts below, we can make a few assumptions: