How to Conduct Sentiment Analysis in R

R Published Updated 3 mins read By Leon Wei
How to Conduct Sentiment Analysis in R cover image

Quick summary

Summarize this blog with AI

Introduction

A lexicon score is a reproducible text feature, not a reliable reading of intent. This lesson uses syuzhet’s supported Bing method on short English examples and deliberately shows negation, mixed sentiment, repeated words, unknown vocabulary and missing input. It does not use the unsupported method="bayes" call from the original guide.

Tested with R 4.5.1 and syuzhet 1.0.7. Run the code blocks in order. Install the named packages before starting; inputs are supplied below.

Score a complete, labeled fixture

Keep each original text and identifier. Do not remove negation words or overwrite source text during cleaning.

reviews <- data.frame(id = 1:7,
  text = c("good", "bad", "not good", "good bad", "flibbertigibbet", "", NA_character_))
score_bing <- function(text) {
  if (!is.character(text) || !is.null(dim(text))) stop("text must be a character vector")
  result <- rep(NA_real_, length(text))
  observed <- !is.na(text)
  if (any(observed)) result[observed] <- syuzhet::get_sentiment(text[observed], method = "bing")
  result
}
reviews$score <- score_bing(reviews$text)
print(reviews)
stopifnot(identical(reviews$score, c(1, -1, 1, 0, 0, 0, NA_real_)))

The wrapper preserves NA rather than treating missing text as a zero score. Positive and negative lexicon entries contribute +1 and −1. In the tested syuzhet version, matching entries are counted once per input string, even if the word repeats.

Use counterexamples to understand the score

The same score can describe very different text. Zero is not a certified neutral opinion.

stopifnot(identical(score_bing(c("good", "good good", "not good")), c(1, 1, 1)))
stopifnot(identical(score_bing(c("good bad", "flibbertigibbet", "")), c(0, 0, 0)))
stopifnot(identical(score_bing(c("GOOD!", "bad...")), c(1, -1)))
reviews$review_note <- c("positive example", "negative example", "negation missed",
                        "opposing words cancel", "no lexicon evidence", "empty input", "missing input")
print(reviews[, c("id", "score", "review_note")])

The implementation matches lexicon entries in the token set, not an occurrence-weighted sum. “Not good” exposes the missing negation model. Longer text, sarcasm, domain vocabulary and language can change how useful the score is. It is not a probability or a calibrated satisfaction scale.

Inspect tokenization instead of deleting punctuation blindly

This ASCII-English token inspection mirrors the package’s default splitting pattern. It is not a universal tokenizer for all languages.

text <- "Not good; GOOD!"
tokens <- strsplit(tolower(text), "[^A-Za-z']+")[[1]]
print(tokens)  # "not" "good" "good"
stopifnot(identical(tokens, c("not", "good", "good")))
stopifnot(identical(score_bing(character(0)), numeric(0)))
stopifnot(identical(score_bing(c(NA_character_, NA_character_)), c(NA_real_, NA_real_)))
for (bad in list(123, matrix("good", 1, 1), factor("good")))
  stopifnot(inherits(tryCatch(score_bing(bad), error = identity), "error"))

The regex contains no invalid single-backslash R escapes. Avoid removing stopwords by default: “not” matters to meaning even though this particular lexicon method cannot use its context. Keep punctuation and casing in the source so a later method can use them.

Validate on the task before aggregating

Define what you want labeled: sentiment toward a product, a specific aspect, or the entire message. Have reviewers label a held-out sample using a written rubric, include ambiguous examples, and compare the model with those labels. Report errors by class and relevant language/domain groups.

Keep missing, empty and unrecognized text separate in any report. Do not average them into apparent neutral satisfaction. If negation or context matters, evaluate a context-aware method against the same held-out sample instead of patching individual examples until they pass. Document the package and lexicon versions, preprocessing and evaluation population. These seven examples expose failure modes; they are not evidence of real-world accuracy.

Primary references

Interview Prep

Begin Your SQL, Python, and R Journey

Master 230 interview-style coding questions and build the data skills needed for analyst, scientist, and engineering roles.

Related Articles

All Articles
How to Remove Outliers in R cover image
r Oct 17, 2026

How to Remove Outliers in R

Learn how to identify and remove outliers in R with this step-by-step guide, featuring detailed code samples for beginners.

How to Use 'countif' in R cover image
r Oct 17, 2026

How to Use 'countif' in R

Unlock the power of 'countif' in R with our comprehensive guide. Perfect for beginners looking to enhance their R programming skills.

How to Use 'abline' in R cover image
r Apr 30, 2024

How to Use 'abline' in R

Unlock the power of 'abline' function in R for data visualization; this guide covers everything from basics to advanced applications with exampl…