Introduction

This exercise applies the workflow from sections 1.1–1.3 of Text Mining with R to the five sample posts supplied in the assignment. The goal is to turn comments into individual words, remove stop words, count word frequencies, and explain what the results could tell a marketer. These five posts are a small classroom sample, so they cannot represent all customers.

1.1: Original text and tidy text

The comments start as strings containing several words. In tidy text, each row contains one token, such as a word, along with an identifier showing which post it came from. A corpus can store documents and their metadata, while a document-term matrix stores documents and terms in rows and columns. Here, a tidy table makes the words easy to filter and count.

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

posts <- tibble(post_id = seq_along(text), text = text)
knitr::kable(posts, caption = "The five original sample posts")
The five original sample posts
post_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.

1.2: Tokenization with unnest_tokens()

Tokenization splits text into units. The function below creates a column called word from the text column. By default, it converts words to lowercase and removes punctuation. Each word keeps its original post_id.

tidy_posts <- posts %>%
  unnest_tokens(word, text)

knitr::kable(head(tidy_posts, 20),
             caption = "First 20 word rows after tokenization")
First 20 word rows after tokenization
post_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

There are 5 posts and 60 word occurrences before stop-word removal.

1.3: Remove stop words and count frequencies

Stop words are common words, such as “the” and “and,” that are often removed for a word frequency analysis. anti_join() removes words that match the stop-word list. The resulting table keeps the remaining words and their post identifiers.

clean_posts <- tidy_posts %>%
  anti_join(tidytext::stop_words, by = "word")

summary_table <- tibble(
  stage = c("Before removing stop words", "After removing stop words"),
  word_occurrences = c(nrow(tidy_posts), nrow(clean_posts))
)
knitr::kable(summary_table, caption = "Word occurrences before and after cleaning")
Word occurrences before and after cleaning
stage word_occurrences
Before removing stop words 60
After removing stop words 26
word_counts <- clean_posts %>%
  count(word, sort = TRUE)

knitr::kable(word_counts, col.names = c("Word", "Occurrences"),
             caption = "Frequency of every remaining word")
Frequency of every remaining word
Word Occurrences
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
repeated_words <- word_counts %>%
  filter(n >= 2)

ggplot(repeated_words, aes(x = n, y = reorder(word, n))) +
  geom_col(fill = "#548B87", width = 0.7) +
  geom_text(aes(label = n), hjust = -0.3) +
  scale_x_continuous(breaks = 0:5, limits = c(0, 5)) +
  labs(
    title = "Most frequent words in the sample posts",
    subtitle = "Words mentioned at least twice",
    x = "Number of occurrences", y = NULL,
    caption = "Source: five posts supplied in the assignment"
  ) +
  theme_minimal(base_size = 13) +
  theme(panel.grid.major.y = element_blank(),
        plot.title = element_text(face = "bold"))
Words appearing at least twice after stop-word removal.

Words appearing at least twice after stop-word removal.

The threshold of two occurrences fits this small dataset. A high threshold designed for entire novels would hide the words in these five short posts.

Findings

“Product” appears four times, while “quality,” “price,” “delivery,” “fast,” “excellent,” and “service” each appear twice. Reading the original posts shows praise for quality and service, along with mixed views about price and delivery, suggesting areas a marketer could investigate further.

Marketing interpretation

The price is called reasonable in one post and expensive in another. Two posts mention fast delivery, while another reports slow shipping. A business could use these comments to guide follow-up questions about value and delivery consistency. The counts alone do not show whether a word is used positively or negatively; this interpretation comes from reading the supplied posts. No sentiment score was calculated.

“Delivery” and “shipping” remain separate words because the analysis counts exact tokens. They can relate to the same topic, but their counts have not been combined.

Challenge and solution

One challenge in this exercise is that single-word counts lose context: “price” appears in both a positive comment and a negative comment. This was addressed by keeping each word’s post ID and checking the original posts before interpreting the results.

Reference

Silge, J., & Robinson, D. (2017). Text mining with R: A tidy approach. O’Reilly Media. Chapter 1, sections 1.1–1.3.

The text-mining workflow is adapted from the linked chapter; the five posts come from the assignment.