One observation I found interesting was how the distribution of word frequencies changed after removing additional words from the analysis. I chose to exclude “trump,” “trump’s,” “top,” “news,” and “ap.” Since the dataset already focuses on news stories mentioning Donald Trump, I removed his name to better identify other recurring topics. I also removed “top,” “news,” and “ap” because they seemed more related to general headline wording or news sources rather than specific issues being covered.
Initially, “trump” and “trump’s” dominated the results, with 18,345 and 5,919 occurrences, respectively. However, after excluding these words, I noticed that there was no longer such a substantial gap between the most frequently occurring words. For example, “iran” appeared 2,840 times, followed closely by “house” at 2,768. This allowed other recurring topics, such as foreign policy, government institutions, elections, and technology, to become more visible.
Although removing these words did not change the frequencies of the remaining terms, it made it easier to identify the different subjects emphasized across the headlines. I also chose to keep words such as “iran,” “war,” “court,” and “ai” because they could provide insight into the issues receiving attention in news coverage. Overall, the results suggest that while the stories share Trump as a common subject, their headlines emphasize a variety of political, international, and technological issues.
# Step 0: Download the data ----
download.file(
url = "https://github.com/drkblake/Data/raw/refs/heads/main/Trump_story_headlines.rds",
destfile = "Trump_story_headlines.rds",
mode = "wb"
)
# Step 1: Install and Load Packages ----
# Install and load the packages needed for
# data management, text mining, and visualization.
if (!require(tidyverse)) {
install.packages("tidyverse")
library(tidyverse)
}
if (!require(tidytext)) {
install.packages("tidytext")
library(tidytext)
}
if (!require(textdata)) {
install.packages("textdata")
library(textdata)
}
if (!require(janitor)) {
install.packages("janitor")
library(janitor)
}
# Step 2: Load Data ----
headlines <- readRDS ("Trump_story_headlines.rds")
#class(headlines)
#View(headlines)
# Step 3: Create a Text-Mining Dataset ----
text_data <- headlines %>%
select(
title
) %>%
filter(
!is.na(title)
) %>%
mutate(
title = str_replace_all(
title,
"[’‘`]",
"'"
)
)
# Step 4: Examine the Dataset ----
set.seed(6600)
sample_rows <- text_data %>%
slice_sample(n = 200)
#View(sample_rows)
# Step 5: Examine the Headline Text ----
sample_headlines <- text_data %>%
slice_sample(n = 100)
#View(sample_headlines)
# Step 6: Create Unigrams ----
unigrams <- text_data %>%
unnest_tokens(
word,
title
)
#View(unigrams)
# Step 7: Count All Unigrams ----
unigram_counts <- unigrams %>%
count(
word,
sort = TRUE
)
unigram_counts
# Step 8: Remove Stop Words ----
# Stop words are common words such as
# "the", "and", and "of" that generally
# contribute little meaning to the analysis.
data("stop_words")
unigrams_clean <- unigrams %>%
anti_join(
stop_words,
by = "word"
)
# Step 9: Remove Social Media Artifacts ----
social_media_noise <- c(
"https",
"http",
"t.co",
"tco",
"co",
"com",
"www",
"amp",
"rt",
"x",
"twitter"
)
unigrams_clean <- unigrams_clean %>%
filter(
!word %in% social_media_noise
)
set.seed(6600)
sample_unigrams_clean <- unigrams_clean %>%
select(
word
) %>%
slice_sample(n = 100)
#View(sample_unigrams_clean)
# Step 10: Count Cleaned Unigrams ----
unigram_counts_clean <- unigrams_clean %>%
count(
word,
sort = TRUE
)
unigram_counts_clean
# Step 11: Remove Additional Custom Words ----
custom_stop_words <- tibble(
word = c(
"trump",
"trump's",
"top",
"news",
"ap"
)
)
unigrams_final <- unigrams_clean %>%
anti_join(
custom_stop_words,
by = "word"
)
# Step 12: Count Final Unigrams ----
# Recount the words after applying all cleaning
# steps and custom exclusions.
unigram_counts_final <- unigrams_final %>%
count(
word,
sort = TRUE
)
unigram_counts_final
# Step 13: View Most Common Words ----
top_words_overall <- unigram_counts_final %>%
slice_max(
n,
n = 25
)
# Reflection Questions
#
# Which words appear most frequently?
#
# Which words seem politically meaningful?
#
# Which words still seem too generic?
#
# Are there words that should be added to the
# custom stop-word list?
#View(top_words_overall)
# Step 14: Create an Overall Word-Frequency Chart ----
top_words_chart_data <- unigram_counts_final %>%
slice_max(
n,
n = 25,
with_ties = FALSE
)
overall_word_chart <- ggplot(
top_words_chart_data,
aes(
x = reorder(word, n),
y = n
)
) +
geom_col(
fill = "gray"
) +
geom_text(
aes(label = n),
hjust = -0.1,
size = 3
) +
coord_flip() +
scale_y_continuous(
expand = expansion(mult = c(0, .10))
) +
labs(
title = "Most Frequently Used Words in Congressional X Posts",
x = "Word",
y = "Number of Occurrences"
) +
theme_minimal()
overall_word_chart