It’s been awhile since I’ve done anything in R and so to get back in the habit I started playing around a bit. I already had twitter api access from a few years back so decided to call up some tweets, run some analysis, create some visualizations, all while learning and becoming more familiar with creating rmarkdown documents.
To begin I needed to pull up some of the required libaries.
library(twitteR)
library(tm)
## Loading required package: NLP
library(wordcloud)
## Loading required package: RColorBrewer
Then I needed to setup authentication with the twitter api.
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
## [1] "Using direct authentication"
So I called for some user timeline info from a few different users through the twitter api.
tweets <- userTimeline("DailyZen", n = 3200)
tweets_text <- sapply(tweets, function(x) x$getText())
zen_corpus <- Corpus(VectorSource(tweets_text))
zen_clean <- tm_map(zen_corpus, removePunctuation)
zen_clean2 <- tm_map(zen_clean, content_transformer(tolower))
zen_clean3 <- tm_map(zen_clean2, removeWords, stopwords("english"))
zen_clean4 <- tm_map(zen_clean3, removeNumbers)
zen_clean5 <- tm_map(zen_clean4, stripWhitespace)
wordcloud(zen_clean5, random.order = F, scale = c(2, 0.25), colors = rainbow(50))
Of course a timeline full of buddhist, zen monk quotes talks about life and things alot. And interestingly the words can and will come up alot from the tweets pulled. So can I do something (like meditate, study, read, etc., yeah), but will I do something now that remains to be seen.
warren <- userTimeline("senwarren", n = 3200)
It is somewhat interesting in the few tweets that a pulled from Senator Elizabeth Warren’s timeline that she #’s or @’s realdonaldtrump alot, presumably, but of course it’s the twitter handle for the sitting president so it’s to be expected.
paul <- userTimeline("randpaul", n = 3200)
Senator Paul Rand on the other hand referneces government, bill’s, #airingofgrievances, and some current events. Also he seemed to reference the Seinfeld show holiday Festivus. Which should always be seen as a plus because it’s a holiday for the rest of us!