Sentiment Analysis using R

Natural Language Processing
Syuzhet package

Simisani Ndaba


October 8, 2022

Meetup Description

After months of postponements and re-appointments, the meetup came to fruition a talk on Sentiment Analysis using different R packages. The meetup aimed at looking at positive and negative sentiments in texts, but also more detailed emotions such as surprise or fear. We learned how to visualise commonly used positive and negative words. The session was suitable for all levels and didn’t need any previous experience with this topic.

Sentiment Analysis using R

Sentiment analysis (also referred to as subjectivity analysis or opinion mining or emotion artificial intelligence) is a NLP technique that identifies important patterns of information and features from a large text corpus.

The session had the following demonstration.


  1. First load the libraries and the dataset required to perform sentiment analysis
#Load libraries




#Load dataset

data<- read.csv("")

#Avoid error related to tolower() invalid multibyte string

data[,sapply(data,is.character)] <- sapply(


  1. The syuzhet package works only on vectors. So, the data was converted to a vector.
#syuzhet package works only on vectors. So, the data was converted to a vector

vector <- as.vector(t(data))
  1. For each book review, scores were determined for different emotions, where a score with value 0 implies the emotion is not associated with the review, and the score of value 1 means there is an association between the emotion and the review. Subsequently, a higher score indicates stronger emotion.
#Sentiment analysis <- get_nrc_sentiment(vector)

emotion.data2 <- cbind(data,
#sentiment score

sentiment.score <- get_sentiment(vector)
Back to top