How to crawl data through twitter and analyze (Sentiment Analysis) it in R

0
1487
Source - R Project website
Source - R Project website

There are many R coders and organizations who want to understand voice of customer through social media and social listening. Many of us would like to analyze twitter data in different ways to come up with sentiment analysis and reasons behind those sentiments. See the example of Batman Vs Superman social media (sentiment analysis) and digital analysis – http://mathminers.com/2016/01/12/movie-superman-vs-batman-digital-media-analytics-battle-who-wins/

Now, you can perform sentiment analysis and find out reasons behind sentiments for users in R or R-studio. You can also segment users based on city, country or any other demographics variables you get through Twitter in R or R-studio. Now main objective becomes how to crawl twitter data and valuable tweets for this analysis through Twitter and R.

Here is a detailed procedure to get tweets data in R –

  1. Go to https://dev.twitter.com/
  2. Click on the tab “Create New App” (sometimes it comes under managing your apps)
  3. After going through typical twitter instructions of app you can get the following
    1. an API Key
    2. API secret,
    3. Access Token
  4. Once you have all the information run the following code to extract data from twitter

install.packages(c(“devtools”, “rjson”, “bit64”, “httr”))

library(devtools)

library(twitteR)

APIkey <- “xxxxx…………………….”
APIsecret <- “xxxxxxxxxx………………………………..”
accesstoken <- “xxxxxx…………………………………………….”
accesstokensecret <- “xxxxxxxxxx…………………………………………………………..”

setup_twitter_oauth(key,apisecret,accesstoken,accesstokensecret)

Once you get the data into twitter data frames, you can use QDAP package to calculate average polarity and sentiment of each tweet. Suppose, you are analyzing the tweets and sentiments of various patients regarding particular drug or medicine or hospital, here is your code –

Pat1$Patientsentiment <- to lower (Pat1$Patientcomment)

Library (qdap)

Sent<-polarity(Pat1$Patientcomment, grouping.var = Pat1$Pat.NUMBER,

         positive.list = positive.words,

         negative.list = negative.words,

         negation.list = negation.words,

         amplification.list = increase.amplification.words,

        rm.incomplete = FALSE, digits = 3)

Patient_Polarity <- data.frame(Sent$all)

 

 

NO COMMENTS

LEAVE A REPLY