This report applies the tidy text methods from Sections 1.1-1.3 of Text Mining with R to the five customer comments provided in the assignment. The goal is to organize the text, remove common stop words, and identify frequently mentioned words. These comments are an instructional sample, so the results do not represent all customers.
Initially, each row contains a complete comment. A comment ID keeps track of the source of each word during analysis.
comments <- tibble(
comment_id = 1:5,
text = c(
"I love the quality of this product. The price is reasonable, and delivery was fast.",
"The product quality is excellent, but the shipping took too long.",
"Great customer service! I would definitely recommend this product to my friends.",
"The price is too expensive. However, the product looks beautiful.",
"Fast delivery and excellent service. I am very satisfied with my purchase."
)
)
knitr::kable(comments, caption = "The five comments supplied in the assignment")
| comment_id | text |
|---|---|
| 1 | I love the quality of this product. The price is reasonable, and delivery was fast. |
| 2 | The product quality is excellent, but the shipping took too long. |
| 3 | Great customer service! I would definitely recommend this product to my friends. |
| 4 | The price is too expensive. However, the product looks beautiful. |
| 5 | Fast delivery and excellent service. I am very satisfied with my purchase. |
The unnest_tokens() function splits the comments into
individual words, converts them to lowercase, and removes punctuation.
Each row now contains one word from one comment.
tidy_comments <- comments %>%
unnest_tokens(word, text)
knitr::kable(head(tidy_comments, 20),
caption = "First 20 word tokens before stop-word removal")
| comment_id | word |
|---|---|
| 1 | i |
| 1 | love |
| 1 | the |
| 1 | quality |
| 1 | of |
| 1 | this |
| 1 | product |
| 1 | the |
| 1 | price |
| 1 | is |
| 1 | reasonable |
| 1 | and |
| 1 | delivery |
| 1 | was |
| 1 | fast |
| 2 | the |
| 2 | product |
| 2 | quality |
| 2 | is |
| 2 | excellent |
Stop words are common words such as “the,” “and,” and “is.” Removing them helps highlight words that describe the product and customer experience. The original comments remain available for interpreting context.
clean_comments <- tidy_comments %>%
anti_join(stop_words, by = "word")
word_counts <- clean_comments %>%
count(word, sort = TRUE) %>%
arrange(desc(n), word)
knitr::kable(word_counts, col.names = c("Word", "Frequency"),
caption = "Word frequencies after stop-word removal")
| Word | Frequency |
|---|---|
| product | 4 |
| delivery | 2 |
| excellent | 2 |
| fast | 2 |
| price | 2 |
| quality | 2 |
| service | 2 |
| beautiful | 1 |
| customer | 1 |
| expensive | 1 |
| friends | 1 |
| love | 1 |
| purchase | 1 |
| reasonable | 1 |
| recommend | 1 |
| satisfied | 1 |
| shipping | 1 |
word_counts %>%
filter(n >= 2) %>%
ggplot(aes(x = n, y = reorder(word, n))) +
geom_col(fill = "steelblue") +
scale_x_continuous(breaks = seq(0, max(word_counts$n), by = 1)) +
labs(
title = "Words Appearing at Least Twice",
subtitle = "Five customer comments, with stop words removed",
x = "Number of occurrences",
y = NULL
) +
theme_minimal(base_size = 12)
After removing stop words, “product” was the most frequent word, appearing four times, while “quality,” “price,” “delivery,” “fast,” “excellent,” and “service” each appeared twice. Reading the original comments alongside these counts shows praise for quality, service, and fast delivery, along with concerns about high prices and slow shipping; word counts alone do not measure sentiment.
One challenge in this analysis is that common words can distract from
the main topics in the comments. I addressed this by using
anti_join(stop_words, by = "word") to remove them before
counting word frequencies.
Silge, J., & Robinson, D. Text Mining with R: A Tidy Approach, Chapter 1, Sections 1.1-1.3. https://www.tidytextmining.com/tidytext
Dataset: Five customer comments supplied in the assignment.