AI helped me write R code to clean the BlueSky dataset, identify common words, and perform sentiment analysis. It made the analysis easier to organize, but I learned that AI-generated code must be checked. One issue was that the dataset was uploaded as a CSV file, so the import code needed to use read_csv() and the correct filename. I also checked for missing posts and duplicate text before analyzing the data.
Human judgment was important when interpreting the results. The Bing sentiment dictionary identifies positive and negative words, but it does not always understand sarcasm, context, advertisements, or the actual meaning of a post. The dataset also contains posts in multiple languages, while the Bing dictionary is English-based. Therefore, I limited sentiment analysis to English posts. The results should be viewed as general patterns in word sentiment, not as a complete measure of customer satisfaction or public opinion.
packages <- c("tidyverse", "tidytext", "plotly")
for (package in packages) {
if (!requireNamespace(package, quietly = TRUE)) {
install.packages(package)
}
}
library(tidyverse)
library(tidytext)
library(plotly)
# Import the CSV file shown in the RStudio Files panel
bluesky <- readr::read_csv(
"my_post (1) (1).csv",
show_col_types = FALSE
)
# Check the data
dim(bluesky)
## [1] 100 23
names(bluesky)
## [1] "uri" "cid" "author_handle" "author_name"
## [5] "text" "author_data" "post_data" "embed_data"
## [9] "reply_count" "repost_count" "like_count" "quote_count"
## [13] "bookmark_count" "indexed_at" "created_at" "in_reply_to"
## [17] "in_reply_root" "quotes" "tags" "mentions"
## [21] "links" "langs" "labels"
head(bluesky$text)
## [1] "In 2022, the Center for Environmental Health tested polyester-spandex sports bras and athletic shirts from Nike, Athleta and others. It found BPA at up to 22 times California's skin-exposure limit. Its advice was simple: change out of your activewear after a workout. #Activewear #BPA #HusbuddyHealth"
## [2] "🔜 Coming soon on Nike US\n👟 Nike SB Air Force 1 \"Cool Grey\"\n🔗 swoosh.supply/2p84e34p ad"
## [3] "Tênis Nike Pegasus Premium\n-Loja Nike\n\nPor apenas R$ 730,16 no Pix\nou R$ 768,59 em 9x sem juros\nAtive o cupom ULTIMAS30\ntidd.ly/488tvIl"
## [4] "gooning"
## [5] "TÊNIS PERFEITO PARA CORRER\n\nTênis Nike Downshifter 14(4 cores)\n\n🔥De R$499 Por R$379\n\n🐙 meli.la/1Ky2isZ"
## [6] "Nike FQ8374-002 Zoom Vapor 16 Academy FG/MG Hombre Glacier Blue/Blue Orbit EU 40\n💥 44,99€ (-50%)\n👉 https://go.descontospt.pt/r/C4q2PpA\n#poupar #descuentos #portugal"
# Make sure the required columns exist
stopifnot("text" %in% names(bluesky))
stopifnot("langs" %in% names(bluesky))
# Remove missing, blank, and duplicate posts
clean_data <- bluesky %>%
filter(!is.na(text), str_trim(text) != "") %>%
mutate(text = str_squish(text)) %>%
distinct(text, .keep_all = TRUE) %>%
mutate(post_id = row_number())
# Display cleaning results
cat("Original number of posts:", nrow(bluesky), "\n")
## Original number of posts: 100
cat("Cleaned number of posts:", nrow(clean_data), "\n")
## Cleaned number of posts: 93
# Check languages
clean_data %>%
count(langs, sort = TRUE)
## # A tibble: 7 × 2
## langs n
## <chr> <int>
## 1 en 45
## 2 <NA> 18
## 3 fr 14
## 4 pt 9
## 5 pt-BR 4
## 6 de 2
## 7 nl 1
english_posts <- clean_data %>%
filter(langs == "en")
cat("Number of English posts:", nrow(english_posts))
## Number of English posts: 45
word_counts <- english_posts %>%
select(post_id, text) %>%
unnest_tokens(word, text) %>%
anti_join(stop_words, by = "word") %>%
filter(str_detect(word, "^[a-z]+$")) %>%
count(word, sort = TRUE)
# Display the 15 most common words
top_words <- word_counts %>%
slice_max(order_by = n, n = 15)
top_words
## # A tibble: 19 × 2
## word n
## <chr> <int>
## 1 nike 39
## 2 cupom 11
## 3 https 10
## 4 check 8
## 5 masculino 8
## 6 mercari 6
## 7 amazon 5
## 8 jersey 5
## 9 mc 5
## 10 oferta 5
## 11 por 5
## 12 trainer 5
## 13 adult 4
## 14 air 4
## 15 de 4
## 16 ebay 4
## 17 sc 4
## 18 sign 4
## 19 uplift 4
# Create a word frequency chart
ggplot(top_words, aes(x = reorder(word, n), y = n)) +
geom_col(fill = "steelblue") +
coord_flip() +
labs(
title = "Most Common Words in BlueSky Posts",
x = "Word",
y = "Frequency"
) +
theme_minimal()
# Load the Bing sentiment dictionary
bing_words <- get_sentiments("bing")
# Break posts into individual words and match sentiment words
sentiment_matches <- english_posts %>%
select(post_id, text) %>%
unnest_tokens(word, text) %>%
inner_join(bing_words, by = "word")
# Count positive and negative words in each post
sentiment_counts <- sentiment_matches %>%
group_by(post_id) %>%
summarise(
positive = sum(sentiment == "positive"),
negative = sum(sentiment == "negative"),
.groups = "drop"
)
# Keep all English posts, including posts with no matched words
post_sentiment <- english_posts %>%
select(post_id, text) %>%
left_join(sentiment_counts, by = "post_id") %>%
mutate(
positive = replace_na(positive, 0L),
negative = replace_na(negative, 0L),
net_sentiment = positive - negative,
sentiment_category = case_when(
net_sentiment > 0 ~ "Positive",
net_sentiment < 0 ~ "Negative",
TRUE ~ "Neutral"
)
)
# Display sentiment results
post_sentiment %>%
count(sentiment_category)
## # A tibble: 3 × 2
## sentiment_category n
## <chr> <int>
## 1 Negative 6
## 2 Neutral 25
## 3 Positive 14
# View individual post scores
post_sentiment %>%
select(text, positive, negative, net_sentiment, sentiment_category) %>%
print(n = 20)
## # A tibble: 45 × 5
## text positive negative net_sentiment sentiment_category
## <chr> <int> <int> <int> <chr>
## 1 "In 2022, the Center for … 0 1 -1 Negative
## 2 "🔜 Coming soon on Nike U… 1 0 1 Positive
## 3 "gooning" 0 0 0 Neutral
## 4 "Nike SB's skate-ready Ai… 3 0 3 Positive
## 5 "#women #safety" 0 0 0 Neutral
## 6 "🛹 Nike SB Air Force 1 \… 1 0 1 Positive
## 7 "Tottenham 2027/28 away k… 0 1 -1 Negative
## 8 "AS NIKE SAYS, \"Just Do … 0 0 0 Neutral
## 9 "I’d rather we had white … 0 0 0 Neutral
## 10 "If I'm Nike I'd be askin… 0 0 0 Neutral
## 11 "Tênis Nike LD-1000 Mascu… 0 0 0 Neutral
## 12 "News Flash, the Director… 3 2 1 Positive
## 13 "It looks like it might b… 1 0 1 Positive
## 14 "Business suit and Nike s… 0 0 0 Neutral
## 15 "Tênis Nike NBA Precision… 0 0 0 Neutral
## 16 "Tênis Nike LeBron Witnes… 0 0 0 Neutral
## 17 "Please and thank you." 1 0 1 Positive
## 18 "yes this is related to t… 0 0 0 Neutral
## 19 "🐍 we are so absolutely … 0 0 0 Neutral
## 20 "New Upcoming Air Max 90 … 0 0 0 Neutral
## # ℹ 25 more rows
sentiment_summary <- post_sentiment %>%
count(sentiment_category) %>%
mutate(
sentiment_category = factor(
sentiment_category,
levels = c("Positive", "Negative", "Neutral")
)
) %>%
arrange(sentiment_category)
sentiment_plot <- ggplot(
sentiment_summary,
aes(
x = sentiment_category,
y = n,
fill = sentiment_category,
text = paste(
"Sentiment:", sentiment_category,
"<br>Number of posts:", n
)
)
) +
geom_col() +
labs(
title = "Sentiment of BlueSky Posts",
x = "Sentiment",
y = "Number of Posts"
) +
theme_minimal()
# Make the chart interactive
ggplotly(sentiment_plot, tooltip = "text")
# Select 10 random posts for manual review
set.seed(123)
post_sentiment %>%
slice_sample(n = min(10, nrow(post_sentiment))) %>%
select(text, positive, negative, net_sentiment, sentiment_category)
## # A tibble: 10 × 5
## text positive negative net_sentiment sentiment_category
## <chr> <int> <int> <int> <chr>
## 1 Nike Tamaneko. (My fellow… 1 0 1 Positive
## 2 Tênis Nike NBA Precision … 0 0 0 Neutral
## 3 Business suit and Nike sn… 0 0 0 Neutral
## 4 gooning 0 0 0 Neutral
## 5 Check out what I'm sellin… 1 0 1 Positive
## 6 🔥 Tênis Nike Uplift Sc M… 1 0 1 Positive
## 7 🔥 Tênis Nike Mc Trainer … 0 0 0 Neutral
## 8 🔥 Tênis Nike Mc Trainer … 0 0 0 Neutral
## 9 youtu.be/85zCwCQPDI8?... 0 0 0 Neutral
## 10 #women #safety 0 0 0 Neutral
# Check sentiment calculations
stopifnot(
all(
post_sentiment$net_sentiment ==
post_sentiment$positive - post_sentiment$negative
)
)
# Check that all posts are included in the sentiment categories
stopifnot(
sum(post_sentiment %>% count(sentiment_category) %>% pull(n)) ==
nrow(post_sentiment)
)
cat("All sentiment checks passed.")
## All sentiment checks passed.
# Show sentiment counts and percentages
sentiment_summary %>%
mutate(
percentage = round(n / sum(n) * 100, 1)
)
## # A tibble: 3 × 3
## sentiment_category n percentage
## <fct> <int> <dbl>
## 1 Positive 14 31.1
## 2 Negative 6 13.3
## 3 Neutral 25 55.6
The analysis identifies frequently used words in the English-language BlueSky posts and categorizes posts as positive, negative, or neutral based on the Bing sentiment dictionary. The results provide an overview of language and sentiment patterns in the dataset, although the dictionary may not fully understand context, sarcasm, or the meaning of individual posts.
Original AI Prompt:
Help me write R code using tidyverse and tidytext to clean a BlueSky dataset, remove missing and duplicate posts, identify common words, and perform sentiment analysis using the Bing sentiment dictionary. Include charts and explain the results.
Improved AI Prompt:
Using R, tidyverse, tidytext, and plotly, analyze a CSV BlueSky dataset containing a text column and a language column. Remove missing and duplicate posts, restrict Bing sentiment analysis to English posts, calculate positive and negative word counts per post, classify posts as positive, negative, or neutral, and create an interactive Plotly chart. Include checks to verify the calculations. Do not assume sentiment scores represent actual customer satisfaction.
AI Evaluation:
AI helped generate and organize the code. I checked the dataset structure, corrected the file import, reviewed the cleaning process, and included checks for the sentiment calculations. I also considered the limitations of the Bing dictionary when interpreting social media posts.
This analysis demonstrates how text mining and sentiment analysis can be used to explore BlueSky posts. The results show patterns in word frequency and sentiment, but human interpretation remains important because automated sentiment tools cannot always understand the full meaning or context of social media posts.