Search code examples
rdataframesentimentr

r sentiment analysis applied to a whole column


I have a dataframe of tweets. A given tweet has multiple sentences. When I use sentimentr's sentiment function, it returns a score for each function like so:

sentiment(as.character(tweets$text[1]))$sentiment
>>> [1] 0.2474874 0.0000000

But if I want a single score for the whole tweet, I can ~accomplish this effect by taking the mean score

mean(sentiment(as.character(tweets$text[1]))$sentiment)
>>>[1] 0.1237437

So, I figured I could apply this same logic to the entire dataframe

tweets$sentiment <- mean(sentiment(as.character((tweets$text)))$sentiment)

But...this returns the same value for all tweets. And if I drop the mean() I get NULL as there are too many sentences/scores to unpack.

How can I get a single value assigned to every row of my dataframe?


Solution

  • We can use sapply to apply sentiment function to each text individually.

    library(sentimentr)
    
    tweets$text <- as.character(tweets$text)
    tweets$sentiment_score <- sapply(tweets$text, function(x) 
                                 mean(sentiment(x)$sentiment))