Code
library(tidytext)
library(janeaustenr)
library(dplyr)
library(stringr)
library(tidyr)
library(ggplot2)The objective of this assignment is to become familiar with sentiment analysis in R, specifically through the workflow introduced in Chapter 2 of Text Mining with R. In particular, the assignment calls for the reproduction of the chapter’s primary sentiment analysis example, followed by an extension of that analysis through the use of a different text corpus and an additional sentiment lexicon.
The primary example from the chapter appears to center on the sentiment analysis of Jane Austen’s novels. In reproducing this example, the text will first be transformed into a tidy format and tokenized into individual words. A sentiment lexicon will be joined to the tokenized text in order to classify words according to sentiment, after which the results will then be summarized and visualized. This portion of the assignment will serve to replicate the core analytical process demonstrated in the chapter.
Once the original example has been successfully reproduced, the analysis will be extended in two ways. Firstly, a different text corpus will be selected and analyzed using a similar workflow, thereby demonstrating how sentiment analysis may be applied beyond the Jane Austen example. One possible corpus for this extended portion may be news article text retrieved via the New York Times API. Secondly, at least one additional sentiment lexicon will be incorporated so that the sentiment results may be compared across differing classification or scoring approaches. In this, the extended portion of the assignment will not only apply the workflow to new data, but will also examine how the choice of lexicon may influence the resulting interpretation.
The analytical approach will likely follow the outlined steps below.
Firstly, the example code from Chapter 2 will be reproduced within this Quarto document, ensuring that it runs successfully and that the original source is properly cited. Subsequently, a second text corpus will be obtained and transformed into a tidy structure suitable for text mining. If the New York Times API is used, relevant article text fields such as headlines, abstracts, or snippets will be extracted for analysis. Sentiment analysis will then be conducted on this second corpus, initially using the same general approach as the original example, and thereafter with an additional sentiment lexicon. Finally, the resulting outputs will be compared in order to identify any notable differences in sentiment patterns, classifications, or overall interpretation.
One expected challenge involves selecting a second corpus that is both manageable and suitable for meaningful comparison. Moreover, because different sentiment lexicons do not all measure sentiment in the same manner, some care will be required when interpreting differences across the results.
As mentioned within the Introduction/Approach section, our first endeavor will pertain to reproducing the primary sentiment analysis found within Chapter 2 of Text Mining with R. In doing so, we will first have to transform Jane Austen’s novels into a tidy format, after which sentiment lexicons will be joined to the tokenized text in order to identify and summarize the sentiments of the author’s words themselves.
Moreover, the example first utilizes the NRC lexicon to examine the joy-associated terms in Emma, and then utilizes the Bing lexicon to calculate the net sentiment across various sections of Austen’s novels.
We will commence by first loading the required libraries.
library(tidytext)
library(janeaustenr)
library(dplyr)
library(stringr)
library(tidyr)
library(ggplot2)Subsequently, we can now create the tidied Jane Austen corpus.
tidy_books <- austen_books() %>%
group_by(book) %>%
mutate(
linenumber = row_number(),
chapter = cumsum(str_detect(text, regex("^chapter [\\divxlc]", ignore_case = TRUE)))
) %>%
ungroup() %>%
unnest_tokens(word, text)
head(tidy_books)# A tibble: 6 × 4
book linenumber chapter word
<fct> <int> <int> <chr>
1 Sense & Sensibility 1 0 sense
2 Sense & Sensibility 1 0 and
3 Sense & Sensibility 1 0 sensibility
4 Sense & Sensibility 3 0 by
5 Sense & Sensibility 3 0 jane
6 Sense & Sensibility 3 0 austen
Next, we will discern the joy terms present in Emma via the NRC lexicon.
nrc_joy <- get_sentiments("nrc") %>%
filter(sentiment == "joy")
emma_joy_words <- tidy_books %>%
filter(book == "Emma") %>%
inner_join(nrc_joy, by = "word") %>%
count(word, sort = TRUE)
head(emma_joy_words, 10)# A tibble: 10 × 2
word n
<chr> <int>
1 good 359
2 friend 166
3 hope 143
4 happy 125
5 love 117
6 deal 92
7 found 92
8 present 89
9 kind 82
10 happiness 76
The output generated above identifies the most common joy-associated words in Emma, thereby reproducing Chapter 2’s example of conducting a sentiment analysis via an inner join.
Now that we have replicated the first half of the primary Chapter 2 example, the next portion entails calculating the net sentiment across the scope of Jane Austen’s novels.
jane_austen_sentiment <- tidy_books %>%
inner_join(get_sentiments("bing"), by = "word") %>%
count(book, index = linenumber %/% 80, sentiment) %>%
pivot_wider(names_from = sentiment, values_from = n, values_fill = 0) %>%
mutate(sentiment = positive - negative)
head(jane_austen_sentiment, 5)# A tibble: 5 × 5
book index negative positive sentiment
<fct> <dbl> <int> <int> <int>
1 Sense & Sensibility 0 16 32 16
2 Sense & Sensibility 1 19 53 34
3 Sense & Sensibility 2 12 31 19
4 Sense & Sensibility 3 15 31 16
5 Sense & Sensibility 4 16 34 18
After determining the net sentiment, we can thereby plot the emotive score through the span of the novels themselves.
ggplot(jane_austen_sentiment, aes(index, sentiment, fill = book)) +
geom_col(show.legend = FALSE) +
facet_wrap(~book, ncol = 2, scales = "free_x")As intended, and as exhibited within Chapter 2 of Text Mining with R, the visualization displays how the net sentiment fluctuates across successive 80-line portions of the various Jane Austen novels.
In extending the original example, a different text corpus will now be introduced through the use of The New York Times article data pertaining to the Artemis II mission. The purpose of this section is to examine the sentiments associated with this body of news text and to compare those findings against the earlier Austen example.
Before the article data may be retrieved and analyzed, the relevant libraries must first be loaded. These packages will in turn serve to assist with tasks such as the API request, JSON parsing, text preparation, and further downstream analysis.
library(httr)
library(jsonlite)
library(tidytext)The next step entails requesting article data from the New York Times Article Search API. In this case, the query will center on the phrase “Artemis II”.
api_key <- Sys.getenv("NYT_API_KEY")
response <- GET(
"https://api.nytimes.com/svc/search/v2/articlesearch.json",
query = list(
q = "Artemis II",
`api-key` = api_key
)
)
status_code(response)[1] 200
content_text <- content(response, "text", encoding = "UTF-8")
cat(content_text){"status":"OK","copyright":"Copyright (c) 2026 The New York Times Company. All Rights Reserved.","response":{"docs":[{"abstract":"","byline":{"original":"By Katrina Miller"},"document_type":"article","headline":{"main":"Before Artemis II, there had to be an Artemis I.","kicker":"","print_headline":""},"_id":"nyt://article/5c936d1f-66c2-5fef-906c-103c137e555f","keywords":[],"multimedia":{"caption":"The moon and the Earth and the Orion spacecraft itself, during the Artemis I mission around the moon in 2022.","credit":"NASA","default":{"url":"https://static01.nyt.com/images/2024/01/09/multimedia/01moon-launch-live-artemis-1-recap/09nasa-moon-04-gpcv-articleLarge.jpg","height":400,"width":600},"thumbnail":{"url":"https://static01.nyt.com/images/2024/01/09/multimedia/01moon-launch-live-artemis-1-recap/09nasa-moon-04-gpcv-thumbStandard.jpg","height":75,"width":75}},"news_desk":"Science","print_page":"","print_section":"","pub_date":"2026-04-01T16:00:06Z","section_name":"Science","snippet":"","source":"The New York Times","subsection_name":"","type_of_material":"Live Blog Post","uri":"nyt://article/5c936d1f-66c2-5fef-906c-103c137e555f","web_url":"https://www.nytimes.com/live/2026/04/01/science/moon-nasa-artemis-launch/before-artemis-ii-there-had-to-be-an-artemis-i","word_count":313},{"abstract":"The Universal Waste Management System aboard the Orion capsule is an innovation in deep space toiletry (it seems to be fixed now).","byline":{"original":"By Katrina Miller"},"document_type":"article","headline":{"main":"The Artemis II Toilet Had a Problem","kicker":"","print_headline":"In First, Artemis II Has a Toilet, But What It Needs Is a Plumber"},"_id":"nyt://article/511b42e0-ca5c-5267-8ab8-9a36f41e7bcc","keywords":[{"name":"Subject","value":"Artemis Program","rank":1},{"name":"Subject","value":"Bathrooms and Toilets","rank":2},{"name":"Organization","value":"National Aeronautics and Space Administration","rank":3},{"name":"Subject","value":"Space and Astronomy","rank":4},{"name":"Subject","value":"Waste Materials and Disposal","rank":5},{"name":"Subject","value":"Rocket Science and Propulsion","rank":6}],"multimedia":{"caption":"An Orion training module, an exact replica of the spacecraft that will take astronauts around the moon, at the Johnson Space Center in Houston. A compartment in the floor houses the only bathroom on board.","credit":"Cassandra Klos for The New York Times","default":{"url":"https://static01.nyt.com/images/2026/04/01/multimedia/01moon-launch-live-bathroom-02-vtgf/01moon-launch-live-bathroom-02-vtgf-articleLarge.jpg","height":429,"width":600},"thumbnail":{"url":"https://static01.nyt.com/images/2026/04/01/multimedia/01moon-launch-live-bathroom-02-vtgf/01moon-launch-live-bathroom-02-vtgf-thumbStandard.jpg","height":75,"width":75}},"news_desk":"Science","print_page":"13","print_section":"A","pub_date":"2026-04-02T02:05:33Z","section_name":"Science","snippet":"The Universal Waste Management System aboard the Orion capsule is an innovation in deep space toiletry (it seems to be fixed now).","source":"The New York Times","subsection_name":"","type_of_material":"News","uri":"nyt://article/511b42e0-ca5c-5267-8ab8-9a36f41e7bcc","web_url":"https://www.nytimes.com/2026/04/01/science/artemis-ii-bathroom-toilet.html","word_count":486},{"abstract":"NASA will broadcast the launch of the Artemis II mission as well as holding live conversations with the crew during their time in space.","byline":{"original":"By Jonathan Wolfe and Ceylan Yeğinsu"},"document_type":"article","headline":{"main":"How to Watch NASA’s Artemis II Moon Launch Online","kicker":"","print_headline":""},"_id":"nyt://article/83a5158b-3bd9-51a9-92b2-990f31623fca","keywords":[{"name":"Subject","value":"Space and Astronomy","rank":1},{"name":"Subject","value":"Artemis Program","rank":2},{"name":"Subject","value":"Rocket Science and Propulsion","rank":3},{"name":"Subject","value":"Content Type: Service","rank":4},{"name":"Subject","value":"Moon","rank":5},{"name":"Organization","value":"National Aeronautics and Space Administration","rank":6},{"name":"Location","value":"Cape Canaveral (Fla)","rank":7}],"multimedia":{"caption":"The Artemis II mission is scheduled to launch on Wednesday evening.","credit":"Kenny Holston/The New York Times","default":{"url":"https://static01.nyt.com/images/2026/04/01/multimedia/01moon-launch-live-how-to-watch-topart-kjpf/01moon-launch-live-how-to-watch-topart-kjpf-articleLarge.jpg","height":900,"width":600},"thumbnail":{"url":"https://static01.nyt.com/images/2026/04/01/multimedia/01moon-launch-live-how-to-watch-topart-kjpf/01moon-launch-live-how-to-watch-topart-kjpf-thumbStandard.jpg","height":75,"width":75}},"news_desk":"Express","print_page":"","print_section":"","pub_date":"2026-04-01T15:00:28Z","section_name":"Science","snippet":"NASA will broadcast the launch of the Artemis II mission as well as holding live conversations with the crew during their time in space.","source":"The New York Times","subsection_name":"","type_of_material":"News","uri":"nyt://article/83a5158b-3bd9-51a9-92b2-990f31623fca","web_url":"https://www.nytimes.com/2026/04/01/science/nasa-artemis-ii-moon-launch-how-to-watch.html","word_count":292},{"abstract":"The four astronauts spoke at a news conference Thursday afternoon at Johnson Space Center in Houston about their journey around the moon and back to Earth.","byline":{"original":"By Kenneth Chang and Adeel Hassan"},"document_type":"article","headline":{"main":"After Artemis II, Astronauts and NASA Look Toward Moon Landing","kicker":"","print_headline":"After Artemis II, Astronauts And NASA Look to Moon Landing"},"_id":"nyt://article/789ebe97-e025-59b3-b217-11a1aa440136","keywords":[{"name":"Subject","value":"Space and Astronomy","rank":1},{"name":"Subject","value":"Artemis Program","rank":2},{"name":"Subject","value":"Moon","rank":3},{"name":"Organization","value":"Canadian Space Agency","rank":4},{"name":"Organization","value":"National Aeronautics and Space Administration","rank":5},{"name":"Organization","value":"Johnson Space Center","rank":6},{"name":"Person","value":"Glover, Victor J Jr","rank":7},{"name":"Person","value":"Hansen, Jeremy (1976- )","rank":8},{"name":"Person","value":"Koch, Christina H","rank":9},{"name":"Person","value":"Wiseman, G Reid","rank":10}],"multimedia":{"caption":"The Artemis II crew, from left, Reid Wiseman, Victor Glover, Christina Koch and Jeremy Hansen, during a news conference at the Johnson Space Center in Houston on Thursday.","credit":"Ronaldo Schemidt/Agence France-Presse — Getty Images","default":{"url":"https://static01.nyt.com/images/2026/04/16/multimedia/16HS-artemis-astronauts/16HP-FLIPPER-ARETEMIS-PRESSER-qjfl-articleLarge.jpg","height":400,"width":600},"thumbnail":{"url":"https://static01.nyt.com/images/2026/04/16/multimedia/16HS-artemis-astronauts/16HP-FLIPPER-ARETEMIS-PRESSER-qjfl-thumbStandard.jpg","height":75,"width":75}},"news_desk":"Science","print_page":"17","print_section":"A","pub_date":"2026-04-16T17:50:59Z","section_name":"Science","snippet":"The four astronauts spoke at a news conference Thursday afternoon at Johnson Space Center in Houston about their journey around the moon and back to Earth.","source":"The New York Times","subsection_name":"","type_of_material":"News","uri":"nyt://article/789ebe97-e025-59b3-b217-11a1aa440136","web_url":"https://www.nytimes.com/2026/04/16/science/artemis-ii-astronauts-moon-mission-press-conference.html","word_count":1355},{"abstract":"","byline":{"original":"By Katrina Miller"},"document_type":"article","headline":{"main":"Meet the four astronauts of Artemis II.","kicker":"","print_headline":""},"_id":"nyt://article/89cb95e8-bda5-5ed9-b00e-7c0abe215c0a","keywords":[],"multimedia":{"caption":"From left, Jeremy Hansen, Christina Koch, Victor Glover and Reid Wiseman at Kennedy Space Center in Florida January.","credit":"Cassandra Klos for The New York Times","default":{"url":"https://static01.nyt.com/images/2026/04/01/multimedia/01moon-launch-live-meet-the-astronauts-all-four-topart-bvjq/01moon-launch-live-meet-the-astronauts-all-four-topart-bvjq-articleLarge.jpg","height":429,"width":600},"thumbnail":{"url":"https://static01.nyt.com/images/2026/04/01/multimedia/01moon-launch-live-meet-the-astronauts-all-four-topart-bvjq/01moon-launch-live-meet-the-astronauts-all-four-topart-bvjq-thumbStandard.jpg","height":75,"width":75}},"news_desk":"Science","print_page":"","print_section":"","pub_date":"2026-04-01T14:01:26Z","section_name":"Science","snippet":"","source":"The New York Times","subsection_name":"Space \u0026 Cosmos","type_of_material":"Live Blog Post","uri":"nyt://article/89cb95e8-bda5-5ed9-b00e-7c0abe215c0a","web_url":"https://www.nytimes.com/live/2026/04/01/science/moon-nasa-artemis-launch/meet-the-four-astronauts-of-artemis-ii","word_count":283},{"abstract":"It has been 50 years since humans last walked the lunar surface, and NASA’s efforts to get back there will take place in stages.","byline":{"original":"By Kenneth Chang"},"document_type":"article","headline":{"main":"Why Artemis II Won’t Land on the Moon","kicker":"","print_headline":""},"_id":"nyt://article/13e1c088-85a2-561c-bb47-7bea18d6478e","keywords":[{"name":"Subject","value":"Moon","rank":1},{"name":"Subject","value":"Space and Astronomy","rank":2},{"name":"Subject","value":"Artemis Program","rank":3},{"name":"Subject","value":"Apollo Project","rank":4},{"name":"Subject","value":"Rocket Science and Propulsion","rank":5}],"multimedia":{"caption":"Edwin “Buzz” Aldrin photographed by Neil Armstrong as they walked across the surface of the moon on July 20, 1969.","credit":"Neil Armstrong/NASA, via Associated Press","default":{"url":"https://static01.nyt.com/images/2026/04/01/multimedia/01moon-launch-live-moon-landing-qkmh/01moon-launch-live-moon-landing-qkmh-articleLarge.jpg","height":606,"width":600},"thumbnail":{"url":"https://static01.nyt.com/images/2026/04/01/multimedia/01moon-launch-live-moon-landing-qkmh/01moon-launch-live-moon-landing-qkmh-thumbStandard.jpg","height":75,"width":75}},"news_desk":"Science","print_page":"","print_section":"","pub_date":"2026-04-01T19:00:07Z","section_name":"Science","snippet":"It has been 50 years since humans last walked the lunar surface, and NASA’s efforts to get back there will take place in stages.","source":"The New York Times","subsection_name":"","type_of_material":"News","uri":"nyt://article/13e1c088-85a2-561c-bb47-7bea18d6478e","web_url":"https://www.nytimes.com/2026/04/01/science/nasa-artemis-ii-astronauts-moon-landing.html","word_count":411},{"abstract":"Quotation of the Day for Sunday, April 19, 2026.","byline":{"original":""},"document_type":"article","headline":{"main":"Quote of the Day: After Artemis II, Astronauts and NASA Look to Moon Landing","kicker":"","print_headline":"Quote of the Day"},"_id":"nyt://article/12d20ee7-08ee-58bd-9428-6ef7e28c0eb1","keywords":[],"multimedia":{"caption":"","credit":"","default":{"url":"","height":0,"width":0},"thumbnail":{"url":"","height":0,"width":0}},"news_desk":"Summary","print_page":"2","print_section":"A","pub_date":"2026-04-19T04:01:08Z","section_name":"Corrections","snippet":"Quotation of the Day for Sunday, April 19, 2026.","source":"The New York Times","subsection_name":"","type_of_material":"News","uri":"nyt://article/12d20ee7-08ee-58bd-9428-6ef7e28c0eb1","web_url":"https://www.nytimes.com/2026/04/19/pageoneplus/quote-of-the-day-after-artemis-ii-astronauts-and-nasa-look-to-moon-landing.html","word_count":50},{"abstract":"","byline":{"original":"By Selam Gebrekidan"},"document_type":"article","headline":{"main":"What China sees as NASA launches Artemis II.","kicker":"","print_headline":""},"_id":"nyt://article/30bac091-930a-5f0b-86c2-42319252b4c6","keywords":[],"multimedia":{"caption":"A miniature model from Chang’e 6, China’s sixth robotic lunar exploration mission at the Beijing Sci-Fi Carnival last week.","credit":"Adek Berry/Agence France-Presse — Getty Images","default":{"url":"https://static01.nyt.com/images/2026/04/01/multimedia/01moon-launch-live-china-race1-wlfb/01moon-launch-live-china-race1-wlfb-articleLarge.jpg","height":406,"width":600},"thumbnail":{"url":"https://static01.nyt.com/images/2026/04/01/multimedia/01moon-launch-live-china-race1-wlfb/01moon-launch-live-china-race1-wlfb-thumbStandard.jpg","height":75,"width":75}},"news_desk":"Science","print_page":"","print_section":"","pub_date":"2026-04-01T23:51:36Z","section_name":"Science","snippet":"","source":"The New York Times","subsection_name":"Space \u0026 Cosmos","type_of_material":"Live Blog Post","uri":"nyt://article/30bac091-930a-5f0b-86c2-42319252b4c6","web_url":"https://www.nytimes.com/live/2026/04/01/science/moon-nasa-artemis-launch/what-china-sees-as-nasa-launches-artemis-ii","word_count":255},{"abstract":"After a successful flight around the moon, the astronauts are relying on a flawed heat shield to protect them as they re-enter Earth’s atmosphere.","byline":{"original":"By Kenneth Chang"},"document_type":"article","headline":{"main":"Coming Home May Be the Most Dangerous Part of Artemis II","kicker":"","print_headline":"For Astronauts, Coming Home May Be the Dangerous Part"},"_id":"nyt://article/6c86d922-0ac6-53a0-aac4-83b04645bf7c","keywords":[{"name":"Subject","value":"Artemis Program","rank":1},{"name":"Subject","value":"Moon","rank":2},{"name":"Organization","value":"National Aeronautics and Space Administration","rank":3},{"name":"Subject","value":"Rocket Science and Propulsion","rank":4},{"name":"Subject","value":"Space and Astronomy","rank":5},{"name":"Subject","value":"Accidents and Safety","rank":7}],"multimedia":{"caption":"","credit":"NASA","default":{"url":"https://static01.nyt.com/images/2026/04/09/science/09artemis-heat-shield-still/09artemis-heat-shield-still-articleLarge.png","height":334,"width":600},"thumbnail":{"url":"https://static01.nyt.com/images/2026/04/09/science/09artemis-heat-shield-still/09artemis-heat-shield-still-thumbStandard.png","height":75,"width":75}},"news_desk":"Science","print_page":"14","print_section":"A","pub_date":"2026-04-09T13:55:14Z","section_name":"Science","snippet":"After a successful flight around the moon, the astronauts are relying on a flawed heat shield to protect them as they re-enter Earth’s atmosphere.","source":"The New York Times","subsection_name":"","type_of_material":"News","uri":"nyt://article/6c86d922-0ac6-53a0-aac4-83b04645bf7c","web_url":"https://www.nytimes.com/2026/04/09/science/nasa-artemis-ii-earth-return-heat-shield.html","word_count":1564},{"abstract":"While SpaceX and Blue Origin get much of the attention among rocket makers these days, traditional aerospace companies, including Boeing and Lockheed Martin, built the vehicles for Artemis II.","byline":{"original":"By Kenneth Chang and Marco Hernandez"},"document_type":"article","headline":{"main":"Who Made the Artemis II Rocket and Space Capsule?","kicker":"","print_headline":""},"_id":"nyt://article/6bb53a82-f0af-5e53-9ada-04bb534e6132","keywords":[{"name":"Subject","value":"Artemis Program","rank":1},{"name":"Subject","value":"Moon","rank":2},{"name":"Subject","value":"Rocket Science and Propulsion","rank":3},{"name":"Subject","value":"Space and Astronomy","rank":4},{"name":"Organization","value":"National Aeronautics and Space Administration","rank":5}],"multimedia":{"caption":"","credit":"New York Times","default":{"url":"https://static01.nyt.com/images/2026/04/01/science/rocket-promo/rocket-promo-articleLarge.jpg","height":299,"width":600},"thumbnail":{"url":"https://static01.nyt.com/images/2026/04/01/science/rocket-promo/rocket-promo-thumbStandard.jpg","height":75,"width":75}},"news_desk":"Science","print_page":"","print_section":"","pub_date":"2026-04-01T19:30:03Z","section_name":"Science","snippet":"While SpaceX and Blue Origin get much of the attention among rocket makers these days, traditional aerospace companies, including Boeing and Lockheed Martin, built the vehicles for Artemis II.","source":"The New York Times","subsection_name":"Space \u0026 Cosmos","type_of_material":"News","uri":"nyt://article/6bb53a82-f0af-5e53-9ada-04bb534e6132","web_url":"https://www.nytimes.com/2026/04/01/science/space/artemis-ii-rocket-space-capsule-boeing-lockheed-martin.html","word_count":345}],"metadata":{"hits":445,"offset":0,"time":146}}}
data <- fromJSON(content_text, flatten = TRUE)
length(data$response$docs)[1] 28
At this stage, the returned API response has been loaded into R. The subsequent stage will involve extracting only the article fields relevant to our sentiment analysis.
Since the analysis is concerned with article text, only the headline, abstract, snippet, and publication date will be retained.
articles <- data$response$docs %>%
select(
headline = headline.main,
abstract,
snippet,
pub_date
)
glimpse(articles)Rows: 10
Columns: 4
$ headline <chr> "Before Artemis II, there had to be an Artemis I.", "The Arte…
$ abstract <chr> "", "The Universal Waste Management System aboard the Orion c…
$ snippet <chr> "", "The Universal Waste Management System aboard the Orion c…
$ pub_date <chr> "2026-04-01T16:00:06Z", "2026-04-02T02:05:33Z", "2026-04-01T1…
The resulting articles data frame now contains the principal text-bearing fields of each article.
In order to prepare the data for tokenization, the headline, abstract, and snippet will now be combined into a single text field.
articles <- articles %>%
mutate(
text = paste(headline, abstract, snippet, sep = " ")
)
articles %>%
select(pub_date, text) %>%
head() pub_date
1 2026-04-01T16:00:06Z
2 2026-04-02T02:05:33Z
3 2026-04-01T15:00:28Z
4 2026-04-16T17:50:59Z
5 2026-04-01T14:01:26Z
6 2026-04-01T19:00:07Z
text
1 Before Artemis II, there had to be an Artemis I.
2 The Artemis II Toilet Had a Problem The Universal Waste Management System aboard the Orion capsule is an innovation in deep space toiletry (it seems to be fixed now). The Universal Waste Management System aboard the Orion capsule is an innovation in deep space toiletry (it seems to be fixed now).
3 How to Watch NASA’s Artemis II Moon Launch Online NASA will broadcast the launch of the Artemis II mission as well as holding live conversations with the crew during their time in space. NASA will broadcast the launch of the Artemis II mission as well as holding live conversations with the crew during their time in space.
4 After Artemis II, Astronauts and NASA Look Toward Moon Landing The four astronauts spoke at a news conference Thursday afternoon at Johnson Space Center in Houston about their journey around the moon and back to Earth. The four astronauts spoke at a news conference Thursday afternoon at Johnson Space Center in Houston about their journey around the moon and back to Earth.
5 Meet the four astronauts of Artemis II.
6 Why Artemis II Won’t Land on the Moon It has been 50 years since humans last walked the lunar surface, and NASA’s efforts to get back there will take place in stages. It has been 50 years since humans last walked the lunar surface, and NASA’s efforts to get back there will take place in stages.
This combined text field will now serve as the corpus for the sentiment analysis.
Now that the single text field has been created, the corpus may be transformed into a tidy format through tokenization.
tidy_articles <- articles %>%
unnest_tokens(word, text)
glimpse(tidy_articles)Rows: 411
Columns: 5
$ headline <chr> "Before Artemis II, there had to be an Artemis I.", "Before A…
$ abstract <chr> "", "", "", "", "", "", "", "", "", "", "The Universal Waste …
$ snippet <chr> "", "", "", "", "", "", "", "", "", "", "The Universal Waste …
$ pub_date <chr> "2026-04-01T16:00:06Z", "2026-04-01T16:00:06Z", "2026-04-01T1…
$ word <chr> "before", "artemis", "ii", "there", "had", "to", "be", "an", …
head(tidy_articles) headline abstract snippet
1 Before Artemis II, there had to be an Artemis I.
2 Before Artemis II, there had to be an Artemis I.
3 Before Artemis II, there had to be an Artemis I.
4 Before Artemis II, there had to be an Artemis I.
5 Before Artemis II, there had to be an Artemis I.
6 Before Artemis II, there had to be an Artemis I.
pub_date word
1 2026-04-01T16:00:06Z before
2 2026-04-01T16:00:06Z artemis
3 2026-04-01T16:00:06Z ii
4 2026-04-01T16:00:06Z there
5 2026-04-01T16:00:06Z had
6 2026-04-01T16:00:06Z to
At this stage, the article corpus has been placed into a one-word structure, making it suitable for joining to the sentiment lexicons.
In order to mirror the afore-presented workflow, the Bing lexicon will be applied to the tokenized article corpus.
bing_sentiment <- tidy_articles %>%
inner_join(get_sentiments("bing"), by = "word") %>%
count(sentiment, sort = TRUE)
bing_sentiment sentiment n
1 positive 8
2 negative 6
The above results provide a straightforward count of the positive and negative words identified within the article corpus. Moreover, we can see that 8 positive words were located, and 6 negative ones. This indicates that the overall media portrayal of the Artemis II mission, at least by The New York Times, could have been more positive than negative.
A simple bar chart can now be created in order to better compare the two Bing sentiment categories more lucidly.
ggplot(data = bing_sentiment, aes(x = sentiment, y = n, fill = sentiment)) +
geom_col(show.legend = FALSE) +
labs(
title = "Bing Sentiment Classification of NYT Artemis II Articles",
x = "Sentiment Category",
y = "Word Count"
)To further extend the analysis, the AFINN lexicon will now be applied to the same corpus. Unlike Bing, AFINN assigns words numerical sentiment scores.
afinn_sentiment <- tidy_articles %>%
inner_join(get_sentiments("afinn"), by = "word")
afinn_summary <- afinn_sentiment %>%
summarise(
total_score = sum(value),
average_score = mean(value),
matched_words = n()
)
afinn_summary total_score average_score matched_words
1 8 0.7272727 11
Having now applied both the Bing and AFINN lexicons to the New York Times article corpus, it can be seen that both point toward a mildly positive overall sentiment. The Bing lexicon identified 8 positive words and 6 negative ones, suggesting that the coverage leans somewhat more positive than negative. The AFINN results support this as well, with a total sentiment score of 8 and an average score of approximately 0.73 across 11 matched words.
Although the two lexicons differ in method, their results are broadly consistent. Bing provides a simple positive and negative classification, whereas AFINN offers a more nuanced score-based measure of sentiment intensity.
Compared to the Jane Austen example, the sentiment within the news article corpus appears more restrained, which is unsurprising given the generally more neutral nature of journalistic writing. Nevertheless, the slight positive skew likely reflects the mission’s historic symbolism and the expression of enthusiasm and anticipation of a shared human goal. As such, this exercise demonstrates that sentiment analysis may yield similar general conclusions across different lexicons, even where the method of measurement differs.
Wickham, H., & Silge, J. (2017). Text mining with R: A tidy approach. O’Reilly Media. https://www.tidytextmining.com/