Reflection

In this lab, I used AI to help write R code to clean my Amazon BlueSky dataset, count words, analyze sentiment, and create an interactive Plotly chart. After removing duplicate rows and missing text, 282 posts remained. One challenge was that the first code removed links beginning with http or https but missed links beginning with www. I improved my prompt to describe this problem and ask for revised code that preserved the original text. Comparing the original and cleaned versions helped me check that the change worked. The step-by-step process made the lab easier to follow, although clearer guidance on handling advertisements would have been helpful.

This assignment showed me that working code does not guarantee meaningful conclusions. The dataset included promotional posts, so frequent words did not necessarily describe customer delivery experiences. The sentiment dictionary also labeled “funny” as negative, even in a post with hashtags such as #lol and #meme. Human judgment was needed to review context, assess relevance, and avoid interpreting word percentages as customer satisfaction percentages. I checked the cleaning arithmetic and compared the sentiment percentages with manual calculations. AI helped produce the code, but reviewing the results was necessary to explain their limitations.

Import the Data

library(readxl)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidytext)
library(stringr)
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
amazon_raw <- read_excel(
  "Amazon_Delivery_SCRAPE 2 EXCEL.xlsx",
  skip = 1
)

nrow(amazon_raw)
## [1] 299
names(amazon_raw)
## [1] "text"             "author_handle"    "timestamp"        "like_count"      
## [5] "repost_count"     "reply_count"      "engagement"       "keyword"         
## [9] "collected_at_utc"

Clean the Data

I removed exact duplicate rows and posts with missing or blank text.

# Remove exact duplicate rows
amazon_unique <- amazon_raw %>%
  distinct()

# Remove missing or blank posts
amazon_clean <- amazon_unique %>%
  dplyr::filter(!is.na(text), str_trim(text) != "") %>%
  mutate(post_id = row_number())

# Check how many rows remain at each step
data.frame(
  Stage = c("Original", "After removing duplicates",
            "After removing missing or blank text"),
  Posts = c(nrow(amazon_raw), nrow(amazon_unique),
            nrow(amazon_clean))
)
##                                  Stage Posts
## 1                             Original   299
## 2            After removing duplicates   290
## 3 After removing missing or blank text   282

Most Common Words

I split the posts into individual words, removed links and common English stop words such as “the” and “and,” and counted word frequency.

mutate(text_for_words = str_remove_all(text, “https?://\S+”)) ## Most Common Words

I improved the link removal after noticing website fragments in the first results. Promotional posts are kept for now and displayed for manual review.

# Keep original text and create a separate version without links
amazon_analysis <- amazon_clean %>%
  mutate(
    text_for_words = str_remove_all(
      text,
      regex("(?:https?://|www\\.)\\S+", ignore_case = TRUE)
    )
  )

# Split cleaned text into lowercase words
amazon_words <- amazon_analysis %>%
  select(post_id, text_for_words) %>%
  unnest_tokens(word, text_for_words)

# Remove common English stop words and tokens without letters
amazon_words_filtered <- amazon_words %>%
  anti_join(stop_words, by = "word") %>%
  dplyr::filter(str_detect(word, "[[:alpha:]]"))

# Count word occurrences
word_counts <- amazon_words_filtered %>%
  count(word, sort = TRUE)

knitr::kable(head(word_counts, 15),
             caption = "15 Most Frequent Words")
15 Most Frequent Words
word n
amazon 267
delivery 161
uk 82
ad 80
price 78
save 78
blu 75
cast 75
imdb 75
ray 75
sale 75
driver 35
drama 30
4k 28
audio 25

Review Possible Promotional Posts

These rules flag possible advertisements for review. They do not establish that a post is irrelevant, and no posts are excluded.

# Look for advertising hashtags or sale phrases
promo_candidates <- amazon_analysis %>%
  dplyr::filter(
    str_detect(
      text,
      regex(
        "#ad\\b|#deals?\\b|#discounts?\\b|amazon\\s*•\\s*uk sale|now only",
        ignore_case = TRUE
      )
    )
  )

# Show the number flagged
nrow(promo_candidates)
## [1] 79
# Show five original posts for manual review
knitr::kable(
  promo_candidates %>%
    select(post_id, text) %>%
    slice_head(n = 5),
  caption = "Possible Promotional Posts: Original Text"
)
Possible Promotional Posts: Original Text
post_id text
21 🔥 NOW ONLY $25

📦 Amazon Has Your Thanksgiving Covered: ONLY $25 & It Includes an 8lb Turkey! 🛒 https://www.amazon.com/fmc/m/40003970/grocery-store-mode/2026thanksgiving?gsc=fe15Lls6uIyOG&linkId=872e100fe6ac5ffe9e173545359d24e6&language=en_US&tag=dgking-20#Deals #DealAlert #Discounts #… | | 38|🔥 AMAZON • UK SALE Teenage Mutant Ninja Turtles Trilogy Blu-ray 💰 Price £19.99 (Save 33%) www.amazon.co.uk/Teenage-Muta… 🔊 (LPCM 2.0) ⭐ IMDb: 6.3 (Action) 🎭 Cast: Robbie Rist, Brian Tochi, Paige Turco, Elias Koteas, Michelan Sisti, Leif Tilden #ad | | 39|🔥 AMAZON • UK SALE 28 Years Later / 28 Years Later: The Bone Temple 2-Movie Collection Blu-ray 💰 Price £15.70 (Save 29%) www.amazon.co.uk/28-Years-Lat… 🔊 (DTS-HD Master Audio 5.1) ⭐ IMDb: 7.3 (Horror) 🎭 Cast: Jack O’Connell, Alfie Williams, Chi Lewis-Parry, Ralph Fiennes, Celi Crossland, #ad | | 40|🔥 AMAZON • UK SALE Avatar: 3-Movie Collection Blu-ray 💰 Price £22.00 (Save 21%) www.amazon.co.uk/Avatar-3-Mov… 🔊 (DTS-HD Master Audio 5.1) ⭐ IMDb: 7.6 (Adventure) 🎭 Cast: Sam Worthington, Zoe Saldaña, Sigourney Weaver, Stephen Lang, Giovanni Ribisi, Joel David Moore #ad | | 41|🔥 AMAZON • UK SALE Planes, Trains & Automobiles 4K Blu-ray 💰 Price £14.99 (Save 25%) www.amazon.co.uk/Planes-Train… 🔊 (DTS-HD Master Audio 5.1) ⭐ IMDb: 8.1 (Comedy) 🎭 Cast: Steve Martin, John Candy, Laila Robins, Michael McKean, Kevin Bacon, Ben Stein #ad | ## Interactive Word Frequency Chart

This chart shows the most frequent words in the collected posts, including promotional content. Hover over each bar to see its count.

top_words <- word_counts %>%
  slice_head(n = 15) %>%
  arrange(n) %>%
  mutate(word = factor(word, levels = word))

plot_ly(
  data = top_words,
  x = ~n,
  y = ~word,
  type = "bar",
  orientation = "h",
  marker = list(color = "#2878B5"),
  hovertemplate = "%{y}: %{x} occurrences<extra></extra>"
) %>%
  layout(
    title = "15 Most Frequent Words in Amazon BlueSky Posts",
    xaxis = list(title = "Word Occurrences"),
    yaxis = list(title = ""),
    margin = list(l = 100)
  )

Sentiment Analysis

I used the Bing sentiment dictionary to identify positive and negative word occurrences. These counts describe matched words, not the number of satisfied or dissatisfied customers.

# Match words before stop-word removal to the sentiment dictionary
bing_dictionary <- get_sentiments("bing")

sentiment_words <- amazon_words %>%
  inner_join(bing_dictionary, by = "word")

# Count positive and negative word occurrences
sentiment_counts <- sentiment_words %>%
  count(sentiment, name = "word_occurrences") %>%
  mutate(
    percentage = round(
      100 * word_occurrences / sum(word_occurrences), 1
    )
  )

knitr::kable(
  sentiment_counts,
  caption = "Sentiment Among Dictionary-Matched Words"
)
Sentiment Among Dictionary-Matched Words
sentiment word_occurrences percentage
negative 144 47.7
positive 158 52.3
# Display the most frequent sentiment words
top_sentiment_words <- sentiment_words %>%
  count(word, sentiment, sort = TRUE)

knitr::kable(
  head(top_sentiment_words, 15),
  caption = "Most Frequent Sentiment Words"
)
Most Frequent Sentiment Words
word sentiment n
master positive 25
stealing negative 12
like positive 11
free positive 8
good positive 7
stole negative 6
controversial negative 5
fast positive 5
funny negative 5
top positive 5
well positive 5
crime negative 4
faster positive 4
love positive 4
perfect positive 4

This method does not account for sarcasm, negation such as “not good,” or whether a positive word describes Amazon delivery. Promotional posts and non-English text can also affect the interpretation.

Verify the Results

I checked the sentiment totals and reviewed original posts to assess whether dictionary labels fit their context.

# Display the sentiment totals clearly
print(as.data.frame(sentiment_counts))
##   sentiment word_occurrences percentage
## 1  negative              144       47.7
## 2  positive              158       52.3
# Check the cleaning arithmetic
299 - 9 - 8
## [1] 282
# Compare original and link-cleaned text for one post with a www link
amazon_analysis %>%
  dplyr::filter(str_detect(text, regex("www\\.", ignore_case = TRUE))) %>%
  select(post_id, text, text_for_words) %>%
  slice_head(n = 1) %>%
  as.data.frame() %>%
  print()
##   post_id
## 1      14
##                                                                                                                                                                                                                                                                                                             text
## 1 Amazon Prime just got a cheaper rival. Walmart is bundling Walmart+ and Sam's Club Plus for $120 a year, undercutting Amazon Prime's $139 fee. Could this be the deal that makes shoppers switch sides?\r\nRead more: www.ibtimes.co.uk/walmart-120-...\r\n#Walmart #AmazonPrime #Retail #Shopping #USBusiness
##                                                                                                                                                                                                                                                                  text_for_words
## 1 Amazon Prime just got a cheaper rival. Walmart is bundling Walmart+ and Sam's Club Plus for $120 a year, undercutting Amazon Prime's $139 fee. Could this be the deal that makes shoppers switch sides?\r\nRead more: \r\n#Walmart #AmazonPrime #Retail #Shopping #USBusiness
# Examine original posts containing the word "funny"
funny_post_ids <- sentiment_words %>%
  dplyr::filter(word == "funny") %>%
  distinct(post_id)

amazon_analysis %>%
  semi_join(funny_post_ids, by = "post_id") %>%
  select(post_id, text) %>%
  slice_head(n = 3) %>%
  as.data.frame() %>%
  print()
##   post_id
## 1      12
## 2      13
## 3      27
##                                                                                                                                                              text
## 1            Fun fact: early in the development cycle of Amazon delivery drones I brought this up in a meeting with the intent of it being funny. Nobody laughed.
## 2                                                                                                                 #delivery #amazon #prime #job #funny #lol #meme
## 3 The dog attacking the Amazon delivery guy was definitely not funny or cute. One of the other ones looks like an actual attack too. This post should be deleted.

Verification Findings

The cleaning arithmetic confirmed that 299 original rows minus 9 duplicate rows and 8 additional rows with missing text left 282 posts. Comparing an original post with its cleaned version showed that the www link was removed while the original text was preserved. Reviewing the sentiment matches revealed that the dictionary labeled “funny” as negative even when it appeared with hashtags such as #lol and #meme. This shows why human judgment is needed to interpret context before drawing conclusions about customer satisfaction.

Findings

After cleaning, 282 posts remained. “Amazon” appeared 267 times and “delivery” appeared 161 times. The rules flagged 79 possible promotional posts, and reviewed examples included product advertisements.

The Bing dictionary identified 158 positive word occurrences (52.3%) and 144 negative word occurrences (47.7%). Positive matches slightly outnumbered negative matches, but promotional content, language differences, and context limit what these results reveal about customer satisfaction.

Manual Calculation Check

There were 158 + 144 = 302 matched word occurrences. Calculating 158 / 302 × 100 gives approximately 52.3%, while 144 / 302 × 100 gives approximately 47.7%. These calculations agree with the R output.