M. Talha Bin Asif
M. Talha Bin Asif

Reputation: 161

Vader Sentiment Analysis in R

I am using Vader Sentiment Analysis on Tweets for a project of mine with 3000 tweets. Everything is working fine when I run the sentiment for only 1 tweet. That gives me all the scores for that tweet but when I run the loop command for all the tweets, I only get the final results as the overall combined score for Vader. I am interested to get the final results as the first one which is giving all the scores. Any help will be highly appreciated.

Sample data:

dput(data_sample$text)
c("Need DoorDash or Uber method asap๐Ÿ˜ญ cause I be starving๐Ÿ˜ญ๐Ÿ˜ญ", 
"Iโ€™m such a real ahh niqq cuz I be having myself weak asl๐Ÿ˜‚", 
"This shii made me laugh so fuccin hard bro๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚", 
"Kevin Hart and Will Ferrell made a Gem in Get hard fr๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚", 
"@_big_emmy @NigerianAmazon Chill๐Ÿคฃ๐Ÿ˜ญ", "Ts so bomedy ๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚", 
"So is that ass Gotdam๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚", 
"This wild๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚", "Idc them late night DoorDashโ€™s be goin crazy๐Ÿคฃ", 
"Video of the week๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚")

Code:

get_vader(data_sample$text[1])

I need this result for all 10 tweets from that loop below:

word_scores                            compound                                 pos 
"{0, 0, 0, 0, 0, 0, 0, 0, 0, -1.8}"                            "-0.421"                                 "0" 
                                neu                                 neg                           but_count 
                            "0.763"                             "0.237"                                 "0" 

Not like this:

for (i in 1:length(data_sample$text)){
  Loop_Error <- F
  tryCatch({ 
    get_vader(data_sample$text[i]) %>%
      as.numeric(unlist(.)) %>%
      .[length(.)-4] ->data_sample$score_vader[i]
  }, error = function(e){
    Loop_Error <<- T})
  if (Loop_Error){
    data_sample$score_vader[i] <- "Error"
  }
}

vader_data



data_sample$score_vader
1                   -0.421
2                   -0.440
3                    0.444
4                   -0.103
5                    0.000
6                    0.000
7                   -0.581
8                    0.000
9                   -0.340
10                   0.000

Upvotes: 0

Views: 1068

Answers (1)

Alik
Alik

Reputation: 103

I have this idea to get all the outputs of data_sample given by get_vader(), but you will need to modify your code a bit to use vader_df():

allvals <- NULL
for (i in 1:length(data_sample)){
outs <-  vader_df(data_sample[i])
allvals <- rbind(allvals,outs)
}

Upvotes: 1

Related Questions