library(twitteR)
library(dplyr)
setup_twitter_oauth(getOption("twitter_consumer_key"), getOption("twitter_consumer_secret"),
getOption("twitter_access_token"), getOption("twitter_access_token_secret"))
## [1] "Using direct authentication"
trump_tweets <- userTimeline("realDonaldTrump", n = 3200)
trump_tweets_df <- bind_rows(lapply(trump_tweets, as.data.frame))
library(stringr)
trump_exclamations <- trump_tweets_df %>%
mutate(exclamation = str_match(text, '[\\.\\"\\!] ([A-Z][^ ]*!)$')[, 2]) %>%
filter(!is.na(exclamation)) %>%
mutate(exclamation = str_to_title(exclamation)) %>%
count(exclamation) %>%
arrange(desc(n))
knitr::kable(trump_exclamations)
| Enjoy! |
12 |
| Thanks! |
6 |
| Great! |
5 |
| Sad! |
4 |
| Bad! |
2 |
| Pathetic! |
2 |
| Watch! |
2 |
| Win! |
2 |
| Amazing! |
1 |
| Cute! |
1 |
| Disgraceful! |
1 |
| Dope! |
1 |
| Fun! |
1 |
| Lazy! |
1 |
| Lies! |
1 |
| Loser! |
1 |
| Losers! |
1 |
| Nice! |
1 |
| Shame! |
1 |
| Think! |
1 |
| Wow! |
1 |
library(ggplot2)
trump_exclamations %>%
mutate(exclamation = reorder(exclamation, desc(n))) %>%
ggplot(aes(exclamation, n)) +
geom_bar(stat = "identity") +
theme_bw() +
ggtitle("Sentiment of @realDonaldTrump Tweets") +
xlab("Sentiment") +
ylab("Frequency") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))

library(wordcloud)
wordcloud(trump_exclamations$end, trump_exclamations$n + 2)
