rsentiment-analysisvader

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

Solution

  • 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)
    }