Basic Analytics with R Activity

Part 1: Extact Data, Favorite Count, Retweeted Count, Wordcloud

set up credentials

library(twitteR)
CONSUMER_SECRET <-"LtZOaw4W6wbaB6grqlmcaeZzBUX0GRLih3YGHskZoRHzvWSxdh"
CONSUMER_KEY <- "wuZ6OITxlU99FjjE2USFa4Zf8"
ACCESS_SECRET <- "09qXsDWA3AiFELPO2OXJGpCvsrdEiCgfxL47MijugXmu5"
ACCESS_TOKEN <- "1390086794901737474-H5hAlP06rhqyolbSQV1lhUEA0hZMXR"

setup_twitter_oauth(consumer_key = CONSUMER_KEY,
                    consumer_secret = CONSUMER_SECRET,
                    access_token = ACCESS_TOKEN,
                    access_secret = ACCESS_SECRET)
## [1] "Using direct authentication"

Set the twitter user

twitterUser <- getUser("vicegandako")

Extract the data

tweets <- searchTwitter("vicegandako", n = 5000, 
                        since = "2021-05-01", 
                        until ="2021-05-07", 
                        lang ="en",
                        retryOnRateLimit = 120)
## Warning in doRppAPICall("search/tweets", n, params = params, retryOnRateLimit =
## retryOnRateLimit, : 5000 tweets were requested but the API can only return 714

Get the favorite and retweeted count

tweets[[1]]$favoriteCount
## [1] 0
tweets[[1]]$retweetCount
## [1] 13

Create a wordcloud

library(twitteR)
library(tm)
## Loading required package: NLP
library(wordcloud)
## Loading required package: RColorBrewer
library(stringr)

Converting the list to dataframe

class(tweets)
## [1] "list"
tweets.df = twListToDF(tweets)
tweets.df$text <- sapply(tweets.df$text,function(x) 
                          iconv(x,to='UTF-8'))

Cleaning up the data

remove_special_char <- str_replace_all(tweets.df$text, "@\\w+", " ")
cleanedtext <- gsub("http.*", " ", remove_special_char)
cleanedtext <- gsub("https.*", " ", cleanedtext)
cleanedtext <- str_replace_all(cleanedtext , "[^[:alnum:]]", " ")
cleanedtext <- str_replace_all(cleanedtext ,"[[^a-zA-Z0-9]]", " ")
textCorpus <- Corpus(VectorSource(cleanedtext))
textCorpus <- tm_map(textCorpus, removePunctuation)
## Warning in tm_map.SimpleCorpus(textCorpus, removePunctuation): transformation
## drops documents
textCorpus <- tm_map(textCorpus, removeNumbers)
## Warning in tm_map.SimpleCorpus(textCorpus, removeNumbers): transformation drops
## documents
textCorpus <- tm_map(textCorpus, content_transformer(tolower))
## Warning in tm_map.SimpleCorpus(textCorpus, content_transformer(tolower)):
## transformation drops documents
textCorpus <- tm_map(textCorpus, removeWords, stopwords("english"))
## Warning in tm_map.SimpleCorpus(textCorpus, removeWords, stopwords("english")):
## transformation drops documents
textCorpus <- tm_map(textCorpus, removeWords, stopwords("SMART"))
## Warning in tm_map.SimpleCorpus(textCorpus, removeWords, stopwords("SMART")):
## transformation drops documents

Plotting the Wordcloud

set.seed(1234)
wordcloud(words = textCorpus, max.words=100, random.order=FALSE,  
          rot.per=0.50, use.r.layout=TRUE, colors = brewer.pal(8,"Dark2"))