Sentiment Analysis Using quanteda in R

Last Updated : 23 Jul, 2025

Sentiment analysis is the technique used to determine the sentiment expressed in the piece of text, classifying it as positive, negative or neutral. In R, the quanteda package is the robust tool for text processing. While sentimentr can be used for sentiment analysis. This article will guide you through the steps to perform the sentiment analysis using these packages.

Now we will discuss the step-by-step implementation of Sentiment Analysis Using quanteda in R Programming Language.

1. Installing and Loading the Required Libraries

We need to install and load the required packages for text processing and sentiment analysis.

  • quanteda: Used for text manipulation and creating a Document-Feature Matrix (DFM).
  • sentimentr: Used to perform sentiment analysis on the text data.
R
install.packages("quanteda")
install.packages("sentimentr")

library(quanteda)
library(sentimentr)

2. Preparing the Text Data

In this step, we will prepare the text data by:

  • Creating a corpus from the text.
  • Tokenizing the corpus which means breaking down the text into individual words or tokens.
  • Removing punctuation and common stopwords (e.g., "the", "is", "and").
R
texts <- c("GeeksforGeeks is a leading platform that provides computer science resources and coding 
                   challenges for programmers and technology enthusiasts, along with interview and exam 
                   preparations for upcoming aspirants. With a strong emphasis on enhancing coding skills and 
                   knowledge, it has become a trusted destination for over 12 million plus registered users worldwide. 
                   The platform offers a vast collection of tutorials, practice problems, interview tutorials, articles, 
                    and courses, covering various domains of computer science.")

corpus <- corpus(texts)

tokens <- tokens(corpus, remove_punct = TRUE)

tokens <- tokens_remove(tokens, stopwords("en"))

dfm <- dfm(tokens)

3. Create a Document-Feature Matrix (DFM)

The Document-Feature Matrix (DFM) is a key structure that captures the frequency of terms across multiple text documents. It is used to analyze word patterns and frequencies. We can extract the top features (most frequent words) in the DFM to understand the most prominent terms in the text dataset.

R
print(topfeatures(dfm, n = 10))

Output:

topfeatures
Document-Feature Matrix (DFM)

4. Visualizing the Text Data

Visualizing the text data helps to better understand sentiment distribution. We can:

  • Create a bar plot of the top 10 most frequent terms.
  • Optionally, generate a word cloud to visually represent the most common terms, where more frequent words are displayed larger.
R
top_terms <- topfeatures(dfm, n = 10)
barplot(top_terms, col = "lightblue", main = "Top 10 Term Frequencies", las = 2)

Output:

barplot
bar plot

4.1. Optionally, Generating a Word Cloud

R
install.packages("wordcloud")
library(wordcloud)

wordcloud(words = names(top_terms), freq = top_terms, min.freq = 1,
          max.words = 200, random.order = FALSE, rot.per = 0.35,
          colors = brewer.pal(8, "Dark2"))

Output:

wordcloud
Word Cloud

5. Performing the Sentiment Analysis

After preparing the data, we can use the sentimentr package to analyze the sentiment of the text. This step will provide sentiment scores which indicate whether the sentiment is positive, negative or neutral.

R
texts_clean <- as.character(corpus)

sentiment_scores <- sentiment(texts_clean)

print(sentiment_scores)

Output:

sentiment_score
Sentiment Analysis

The sentiment scores are calculated for each sentence or text entry, giving us insights into the overall tone of the content.

Conclusion

In this article, we can demonstrated how to perform the sentiment analysis in the R using quanteda package for the text processing and the sentimentr package for the sentiment analysis. By following these steps, we can effectively analyze the sentiment expressed in the text data, making it easier to the gain insights from textual content.

Comment

Explore