When it comes to headlines dealing with Donald Trump in this analysis, what immediately sticks out is that Iran was the top topic in headlines which could be closely associated with war which is at third. What this goes to show is that many headlines that deal with Donald Trump as of late have been related to the war with Iran, and obviously with the nature of that topic, it obviously attracts a lot of stories. Another observation I made is that “house” and “senate” were both in the top ten topics as well. Trump’s relationship with Congress has been under a big spotlight as of late, and this evidence helps to validate that line of thinking in a big way.

As far as words that I chose to have exempt from the list, I felt that including very common terms like “says”, “current” and “news” was unnecessary since words like those are going to be featured in nearly every headline regardless. It felt redundant to keep those included which is why I wanted them exempt. I also specifically chose to exclude any words that had possession such as “trump’s” since that could lead to repeated results that do not go anywhere. “Trump” and “Donald” obviously needed to be left out as well since that would be in virtually every headline as well and does not give any useful information when it comes to figuring out whether or not a topic is being brought up often in news coverage. This changed the outcome by giving me quality information since it was largely all that was left to sort through and was not just an obvious thing that would have been included when it was not needed. The graph is below demonstrating my final results for this report.

Code

The code that I used for my analysis and results is demonstrated below.

library(tidyverse)
library(tidytext)

# Read data
headlines <- readRDS("Trump_story_headlines.rds")

headlines <- headlines %>%
  mutate(
    title = str_replace_all(title, "'s\\b", ""),
    title = str_replace_all(title, "’s\\b", "")
  )

unigrams <- headlines %>%
  unnest_tokens(word, title)

# Tokenize the title column into unigrams
unigrams <- headlines %>%
  unnest_tokens(word, title)

# Load stop words
data("stop_words")
custom_remove <- tibble(
  word = c(
    "trump's",
    "s",
    "donald",
    "president",
    "former"
  )
)

# Additional words to remove
custom_remove <- tibble(
  word = c(
    "trump", "donald", "president", "former",
    "says", "said", "news", "new", "rt",
    "amp", "https", "http", "co", "tco",
    "us", "u", "current", "two", "will", "could", "may"
  )
)

# Clean and filter
clean_unigrams <- unigrams %>%
  anti_join(stop_words, by = "word") %>%
  anti_join(custom_remove, by = "word") %>%
  filter(
    !str_detect(word, "^\\d+$"),
    str_length(word) > 2
  )

# Count frequencies
word_counts <- clean_unigrams %>%
  count(word, sort = TRUE)

# Display top 25
word_counts %>%
  slice_max(n, n = 25)
word_counts %>%
  slice_max(n, n = 25) %>%
  mutate(word = reorder(word, n)) %>%
  ggplot(aes(x = word, y = n)) +
  geom_col(fill = "steelblue") +
  coord_flip() +
  labs(
    title = "Top 25 Most Common Unigrams in Trump Headlines",
    x = "Word",
    y = "Frequency"
  ) +
  theme_minimal()