Sentiment analysis of Trump’s tweets with R

August 18, 2016

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

Data Scientist David Robinson caused a bit of a stir in the media when he analyzed Donald Trump's tweets and revealed that those sent from an Android device were likely sent by the candidate himself, while those sent from an iPhone were likely sent by campaign staffers. The difference? As seen in the chart below, Android-based tweets used angrier, negative words while iPhone-based tweets tended be straightforward campaign announcements and hashtag promotions. The news was reported in Scientific American, the LA Times, PC Magazine and David even gave an interview with Time magazine


David used the R language and several contributed packages to analyze Trump's tweets. (The R code behind the analysis is available on Github as an R Markdown document, which also makes an excellent example of literate programming with R.) He used the twitteR package and the userTimeline function to download tweets and metadata from the @realDonaldTrump account, which formed the raw data for the analysis. The tidytext package function unnest_tokens extracted and standardized individual words from the tweets, from which simple tabulation generated the chart above of words more frequently used by iPhone and Android tweets. The tidytext package was also used to measure the sentiment of the words used, and again there was a clear difference between iPhone and Android tweets in use of words related to sadness, fear, anger and disgust; and surprise, anticipation, trust and joy.

Trump sentiments

For more details on David's analysis of the Trump tweets, and a fascinating hypothesis that an iPhone-based staffer is attempting to emulate the style of the real Trump's tweets, check out his blog post linked below.

Variance Explained: Text analysis of Trump's tweets confirms he writes only the (angrier) Android half

To leave a comment for the author, please follow the link and comment on their blog: Revolutions. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


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)