This lab shows how AI can help organize an R Markdown report and write code for cleaning text, counting words, scoring sentiment, and creating an interactive chart. During the process, my first attempt to knit the report produced a formatting error, which showed that AI-generated instructions still require careful copying and troubleshooting. The example uses simulated posts rather than actual BlueSky data, so its findings cannot be treated as evidence about real customers. I think the lab provides useful practice with AI and R, but a sample dataset and a checklist for verifying results would make the instructions easier to follow.
Human judgment remains necessary when choosing data, evaluating sentiment labels, and interpreting results. This report uses a small, manually defined sentiment dictionary, which makes the calculations easy to inspect but limits the words the analysis can recognize. A word-count method can miss sarcasm, negation, and context. For example, it would score “not good” positively because it recognizes “good” without understanding “not.” The report therefore includes data checks and hand-calculated examples to help evaluate the AI-written code. Any difference between the two simulated weeks reflects how these example posts were constructed, rather than a verified change in customer opinions.
This practice report analyzes 20 simulated BlueSky-style posts about a fictional coffee shop across two example weeks.
These posts are invented. They were not collected from BlueSky. This is a demonstration of the workflow. The assignment requests collected group data, so this example should only be submitted if simulated data is allowed.
The analysis asks:
posts <- tibble(
post_id = 1:20,
week = rep(c("Week 1", "Week 2"), each = 10),
text = c(
"I love this coffee shop. The latte was excellent.",
"Friendly staff and delicious coffee.",
"The service was slow and the coffee was cold.",
"Great atmosphere and a wonderful pastry.",
"My drink was bitter and disappointing.",
"Fresh muffins and amazing service.",
"The tables were dirty and the music was annoying.",
"Good coffee and a comfortable seat.",
"My order was wrong. A terrible experience.",
"Stopped in for a coffee before work.",
"Excellent coffee and friendly service today.",
"I love the new menu. The cookies are delicious.",
"Great service and a wonderful latte.",
"The coffee was cold and the wait was frustrating.",
"Amazing pastries and a beautiful space.",
"Good drinks and helpful staff.",
"My sandwich was disappointing and expensive.",
"A pleasant visit with fresh coffee.",
"Happy with my order. Everything was perfect.",
"Ordered a latte and sat near the window."
)
)
knitr::kable(posts)
| post_id | week | text |
|---|---|---|
| 1 | Week 1 | I love this coffee shop. The latte was excellent. |
| 2 | Week 1 | Friendly staff and delicious coffee. |
| 3 | Week 1 | The service was slow and the coffee was cold. |
| 4 | Week 1 | Great atmosphere and a wonderful pastry. |
| 5 | Week 1 | My drink was bitter and disappointing. |
| 6 | Week 1 | Fresh muffins and amazing service. |
| 7 | Week 1 | The tables were dirty and the music was annoying. |
| 8 | Week 1 | Good coffee and a comfortable seat. |
| 9 | Week 1 | My order was wrong. A terrible experience. |
| 10 | Week 1 | Stopped in for a coffee before work. |
| 11 | Week 2 | Excellent coffee and friendly service today. |
| 12 | Week 2 | I love the new menu. The cookies are delicious. |
| 13 | Week 2 | Great service and a wonderful latte. |
| 14 | Week 2 | The coffee was cold and the wait was frustrating. |
| 15 | Week 2 | Amazing pastries and a beautiful space. |
| 16 | Week 2 | Good drinks and helpful staff. |
| 17 | Week 2 | My sandwich was disappointing and expensive. |
| 18 | Week 2 | A pleasant visit with fresh coffee. |
| 19 | Week 2 | Happy with my order. Everything was perfect. |
| 20 | Week 2 | Ordered a latte and sat near the window. |
First, check the number of posts, missing values, blank text, duplicate text, and duplicate IDs.
data_checks <- posts %>%
summarise(
total_posts = n(),
missing_text = sum(is.na(text)),
blank_text = sum(str_trim(text) == "", na.rm = TRUE),
duplicate_text = sum(duplicated(text)),
duplicate_ids = sum(duplicated(post_id))
)
knitr::kable(data_checks)
| total_posts | missing_text | blank_text | duplicate_text | duplicate_ids |
|---|---|---|---|---|
| 20 | 0 | 0 | 0 | 0 |
knitr::kable(count(posts, week))
| week | n |
|---|---|
| Week 1 | 10 |
| Week 2 | 10 |
The expected result is 20 posts, 10 per week, with no missing text, blank text, duplicate text, or duplicate IDs.
The cleaning code removes missing or blank posts, standardizes spaces, and removes repeated text within each week.
clean_posts <- posts %>%
filter(!is.na(text)) %>%
mutate(text = str_squish(text)) %>%
filter(text != "") %>%
distinct(week, text, .keep_all = TRUE)
stopifnot(
nrow(clean_posts) == 20L,
!anyDuplicated(clean_posts$post_id),
all(count(clean_posts, week)$n == 10L)
)
knitr::kable(
tibble(
posts_before_cleaning = nrow(posts),
posts_after_cleaning = nrow(clean_posts)
)
)
| posts_before_cleaning | posts_after_cleaning |
|---|---|
| 20 | 20 |
No rows should be removed because this example dataset was constructed without those data problems.
Tokenization splits each post into lowercase words. For word-frequency analysis, common stop words such as “the” and “and” are removed.
tokens <- clean_posts %>%
unnest_tokens(word, text)
frequency_tokens <- tokens %>%
anti_join(tidytext::stop_words, by = "word") %>%
filter(str_detect(word, "[a-z]"))
top_words <- frequency_tokens %>%
count(word, sort = TRUE) %>%
arrange(desc(n), word) %>%
slice_head(n = 10)
knitr::kable(top_words)
| word | n |
|---|---|
| coffee | 8 |
| service | 4 |
| latte | 3 |
| amazing | 2 |
| cold | 2 |
| delicious | 2 |
| disappointing | 2 |
| excellent | 2 |
| fresh | 2 |
| friendly | 2 |
ggplot(
top_words,
aes(x = reorder(word, n), y = n)
) +
geom_col(fill = "#8B6551") +
coord_flip() +
labs(
title = "Most Frequent Words in the Simulated Posts",
x = NULL,
y = "Word occurrences"
) +
theme_minimal()
These counts show repeated topics in the example. A frequent word does not necessarily indicate positive or negative sentiment. Related words such as “pastry” and “pastries” are counted separately.
This demonstration uses a small, manually defined dictionary rather than a standard validated sentiment lexicon. It requires no additional dictionary download.
Each matched positive word contributes +1 and each matched negative word contributes -1. Words outside the dictionary contribute zero.
positive_words <- c(
"love", "excellent", "friendly", "delicious",
"great", "wonderful", "fresh", "amazing",
"good", "comfortable", "beautiful", "helpful",
"pleasant", "happy", "perfect"
)
negative_words <- c(
"slow", "cold", "bitter", "disappointing",
"dirty", "annoying", "wrong", "terrible",
"frustrating", "expensive"
)
sentiment_dictionary <- tibble(
word = c(positive_words, negative_words),
sentiment = c(
rep("positive", length(positive_words)),
rep("negative", length(negative_words))
),
value = c(
rep(1, length(positive_words)),
rep(-1, length(negative_words))
)
)
stopifnot(!anyDuplicated(sentiment_dictionary$word))
knitr::kable(sentiment_dictionary)
| word | sentiment | value |
|---|---|---|
| love | positive | 1 |
| excellent | positive | 1 |
| friendly | positive | 1 |
| delicious | positive | 1 |
| great | positive | 1 |
| wonderful | positive | 1 |
| fresh | positive | 1 |
| amazing | positive | 1 |
| good | positive | 1 |
| comfortable | positive | 1 |
| beautiful | positive | 1 |
| helpful | positive | 1 |
| pleasant | positive | 1 |
| happy | positive | 1 |
| perfect | positive | 1 |
| slow | negative | -1 |
| cold | negative | -1 |
| bitter | negative | -1 |
| disappointing | negative | -1 |
| dirty | negative | -1 |
| annoying | negative | -1 |
| wrong | negative | -1 |
| terrible | negative | -1 |
| frustrating | negative | -1 |
| expensive | negative | -1 |
The labels reflect choices that a human analyst should review. For example, “cold” is negative here because it describes an unwanted cold coffee, but it could be appropriate in a post about iced coffee.
Sentiment scoring uses all word tokens, before stop-word removal. Keeping all tokens makes it easier to inspect context later, although this simple scoring rule still does not interpret that context.
Posts with no dictionary matches receive a score of zero.
matched_words <- tokens %>%
inner_join(sentiment_dictionary, by = "word")
scores <- matched_words %>%
group_by(post_id) %>%
summarise(
positive_matches = sum(value == 1),
negative_matches = sum(value == -1),
net_sentiment = sum(value),
.groups = "drop"
)
post_scores <- clean_posts %>%
left_join(scores, by = "post_id") %>%
mutate(
across(
c(positive_matches, negative_matches, net_sentiment),
~ replace_na(.x, 0)
),
classification = case_when(
net_sentiment > 0 ~ "Positive score",
net_sentiment < 0 ~ "Negative score",
TRUE ~ "Zero score"
)
)
knitr::kable(post_scores)
| post_id | week | text | positive_matches | negative_matches | net_sentiment | classification |
|---|---|---|---|---|---|---|
| 1 | Week 1 | I love this coffee shop. The latte was excellent. | 2 | 0 | 2 | Positive score |
| 2 | Week 1 | Friendly staff and delicious coffee. | 2 | 0 | 2 | Positive score |
| 3 | Week 1 | The service was slow and the coffee was cold. | 0 | 2 | -2 | Negative score |
| 4 | Week 1 | Great atmosphere and a wonderful pastry. | 2 | 0 | 2 | Positive score |
| 5 | Week 1 | My drink was bitter and disappointing. | 0 | 2 | -2 | Negative score |
| 6 | Week 1 | Fresh muffins and amazing service. | 2 | 0 | 2 | Positive score |
| 7 | Week 1 | The tables were dirty and the music was annoying. | 0 | 2 | -2 | Negative score |
| 8 | Week 1 | Good coffee and a comfortable seat. | 2 | 0 | 2 | Positive score |
| 9 | Week 1 | My order was wrong. A terrible experience. | 0 | 2 | -2 | Negative score |
| 10 | Week 1 | Stopped in for a coffee before work. | 0 | 0 | 0 | Zero score |
| 11 | Week 2 | Excellent coffee and friendly service today. | 2 | 0 | 2 | Positive score |
| 12 | Week 2 | I love the new menu. The cookies are delicious. | 2 | 0 | 2 | Positive score |
| 13 | Week 2 | Great service and a wonderful latte. | 2 | 0 | 2 | Positive score |
| 14 | Week 2 | The coffee was cold and the wait was frustrating. | 0 | 2 | -2 | Negative score |
| 15 | Week 2 | Amazing pastries and a beautiful space. | 2 | 0 | 2 | Positive score |
| 16 | Week 2 | Good drinks and helpful staff. | 2 | 0 | 2 | Positive score |
| 17 | Week 2 | My sandwich was disappointing and expensive. | 0 | 2 | -2 | Negative score |
| 18 | Week 2 | A pleasant visit with fresh coffee. | 2 | 0 | 2 | Positive score |
| 19 | Week 2 | Happy with my order. Everything was perfect. | 2 | 0 | 2 | Positive score |
| 20 | Week 2 | Ordered a latte and sat near the window. | 0 | 0 | 0 | Zero score |
A zero score can mean no dictionary matches or an equal number of positive and negative matches. It does not prove that a post is neutral.
Post 1 contains “love” and “excellent,” so its expected score is +2.
Post 3 contains “slow” and “cold,” so its expected score is -2.
Post 10 contains no dictionary words, so its expected score is 0.
manual_checks <- tibble(
post_id = c(1L, 3L, 10L),
expected_score = c(2, -2, 0)
) %>%
left_join(
post_scores %>% select(post_id, net_sentiment),
by = "post_id"
) %>%
mutate(passed = expected_score == net_sentiment)
knitr::kable(manual_checks)
| post_id | expected_score | net_sentiment | passed |
|---|---|---|---|
| 1 | 2 | 2 | TRUE |
| 3 | -2 | -2 | TRUE |
| 10 | 0 | 0 | TRUE |
stopifnot(all(manual_checks$passed))
matched_words %>%
filter(post_id %in% c(1, 3)) %>%
select(post_id, word, sentiment, value) %>%
knitr::kable()
| post_id | word | sentiment | value |
|---|---|---|---|
| 1 | love | positive | 1 |
| 1 | excellent | positive | 1 |
| 3 | slow | negative | -1 |
| 3 | cold | negative | -1 |
stopifnot(
nrow(post_scores) == nrow(clean_posts),
sum(post_scores$positive_matches) ==
sum(matched_words$value == 1),
sum(post_scores$negative_matches) ==
sum(matched_words$value == -1),
sum(post_scores$net_sentiment) ==
sum(matched_words$value)
)
cat("All programmed verification checks passed.")
## All programmed verification checks passed.
These checks verify selected calculations and accounting totals. They do not establish that the dictionary correctly understands every post.
weekly_summary <- post_scores %>%
group_by(week) %>%
summarise(
posts = n(),
positive_matches = sum(positive_matches),
negative_matches = sum(negative_matches),
net_sentiment = sum(net_sentiment),
average_score = mean(net_sentiment),
.groups = "drop"
)
knitr::kable(weekly_summary, digits = 2)
| week | posts | positive_matches | negative_matches | net_sentiment | average_score |
|---|---|---|---|---|---|
| Week 1 | 10 | 10 | 8 | 2 | 2 |
| Week 2 | 10 | 14 | 4 | 10 | 10 |
Using this dictionary, Week 1 has a net score of 2 and Week 2 has a net score of 10.
Each week contains 10 posts. The average score per post allows a clearer comparison if the number of posts changes in a future dataset. It still depends on post length and dictionary coverage.
Hover over a bar to see the average score, number of posts, and matched-word counts.
chart_data <- weekly_summary %>%
mutate(
hover_text = paste0(
week,
"<br>Posts: ", posts,
"<br>Average score: ", round(average_score, 2),
"<br>Positive matches: ", positive_matches,
"<br>Negative matches: ", negative_matches
)
)
plot_ly(
data = chart_data,
x = ~week,
y = ~average_score,
type = "bar",
text = ~hover_text,
hoverinfo = "text",
marker = list(color = c("#A88770", "#58786B"))
) %>%
layout(
title = "Sentiment in Simulated Coffee Shop Posts",
xaxis = list(title = "Example week"),
yaxis = list(
title = "Average net sentiment score per post",
zeroline = TRUE
)
)
The code provides a reproducible demonstration, but the interpretation requires human review.
For real BlueSky data, I would review a sample of posts manually, compare those labels with the automated scores, investigate disagreements, and document how the posts were collected. I would also compare this custom dictionary with an established sentiment method.
The following requests were used during this conversation.
Initial request:
i dont have anything. come up wiht an example and walk me thru step by step
AI response summary: The assistant proposed clearly labeled, simulated coffee-shop posts and began guiding the creation of an HTML R Markdown report.
Follow-up request:
just give me something i can press cntrl a and delete it all and copy everything else into the top lef t
AI response summary: The assistant supplied a complete replacement R Markdown document, including data checks, text analysis, a custom sentiment dictionary, manual verification, and an interactive plotly chart.
These requests helped obtain an example and a complete document, but they did not precisely specify packages or verification requirements. A stronger follow-up would explicitly request the packages, input structure, expected output, and checks. Screenshots of the actual conversation should accompany the discussion submission.