0

I am trying to use nrguimaraes's VADER sentiment tool on R to get the sentiment scores of several tweets. The description of the tool and how to install it is described here: https://rdrr.io/github/nrguimaraes/sentimentSetsR/man/getVaderRuleBasedSentiment.html

However, when I try to process more than one element, it comes out with an error. I would like to edit the function so that it is able to handle several elements.

The function is:

function (text, compound = TRUE) 
{
    text <- iconv(text, to = "UTF-8")
    text <- gsub("\\p{So}|\\p{Cn}", "", text, perl = TRUE)
    text <- gsub("\\s+", " ", text)
    if (is.na(text)) {
        return(NA)
    }
    if (text == "" || text == " ") {
        return(NA)
    }
    return(PolarityScores(text, compound))
}

So when I run

getVaderRuleBasedSentiment(file$text)

The error comes out as

Warning message:
In if (is.na(text)) { :
  the condition has length > 1 and only the first element will be used

I'm wondering how I can edit this function to be able to handle data that has several rows of texts, such as when you collect tweets using Rtweet.

sharkatt
  • 13
  • 5
  • just vectorize your function: ie `getVaderRuleBasedSentimentVec = Vectorize(getVaderRuleBasedSentiment)` then use the vectorized function: `getVaderRuleBasedSentimentVec(file$text)` – Onyambu Aug 15 '19 at 23:46
  • Not sure what *file* is but if a data frame, you are passing a vector of more than one element into `is.na` which returns a boolean/logical vector of same length. But `if` tests for one length conditions. Hence, the warning not error. – Parfait Aug 15 '19 at 23:46

0 Answers0