Purpose and approach

This report applies Sections 1.1 through 1.3 of Text Mining with R to the five customer comments provided in the assignment. The analysis converts comments into individual words, removes common stop words, and counts and visualizes word frequencies.

1. Organizing the comments

Initially, each comment is a single text string. The data table preserves a comment ID so each word can be traced to its original comment.

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)
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.

2. Converting text into tidy words

unnest_tokens() splits the comments into words, converts them to lowercase, and removes punctuation. The resulting table has one word per row, while retaining the comment ID.

tidy_comments <- comments %>%
  unnest_tokens(word, text)
knitr::kable(head(tidy_comments, 20))
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

3. Removing stop words and counting words

Common words such as “the” and “and” can obscure the topics customers discuss. anti_join() removes words included in the tidytext stop-word list before the remaining words are counted.

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"))
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
ggplot(word_counts, aes(x = reorder(word, n), y = n)) +
  geom_col(fill = "#287D8E", width = 0.75) +
  coord_flip() +
  scale_y_continuous(breaks = seq(0, max(word_counts$n), by = 1)) +
  labs(
    title = "Word Frequencies in Five Customer Comments",
    subtitle = "After removing common stop words",
    x = NULL,
    y = "Number of occurrences"
  ) +
  theme_minimal(base_size = 12)

Findings

“Product” was the most frequent remaining word, appearing four times, while “quality,” “price,” “delivery,” “fast,” “excellent,” and “service” each appeared twice. Reading the original comments alongside these counts reveals praise for quality and service, as well as concerns about high prices and slow shipping.

Challenge and solution

A challenge in this workflow is that common words can dominate the results and make the main topics harder to identify. Removing stop words with anti_join(stop_words, by = "word") makes the frequency table easier to interpret.

Limitations

These results describe only the five supplied comments. Word frequency does not determine sentiment: for example, the two comments mentioning price express different opinions, so the original wording is needed to interpret the counts.

Method reference

Julia Silge and David Robinson, Text Mining with R, Sections 1.1–1.3.