Load required packages
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(httr)
library(jsonlite)
##
## Attaching package: 'jsonlite'
##
## The following object is masked from 'package:purrr':
##
## flatten
Making a GET Request from the New York Times Best Sellers List
base_url <- 'https://api.nytimes.com/svc/books/v3/lists.json'
api_key <- 'O7juBZuQkqRBleiDZZwtXNZ8iGxehYcq'
api_params <- list(
'api-key' = api_key,
'list' = 'hardcover-fiction',
'bestsellers-date' = '2023-10-11'
)
response <- GET(url = base_url, query = api_params)
results <- fromJSON(content(response, "text"))
Examining the DataFrame
# There are 5 columns, status, copyright, num_results, last_modified, & results
# results$status is the status code, the code received OK which means the request is successful
# results$copyright is the copyright information
# results$num_results contains the number oh hardcover fiction books on the list during October 11 2023 which is 15
# results$last_modified contains the date the list was last modified
# results$results is the dataframe and contains the data we're interested in
head(results)
## $status
## [1] "OK"
##
## $copyright
## [1] "Copyright (c) 2023 The New York Times Company. All Rights Reserved."
##
## $num_results
## [1] 15
##
## $last_modified
## [1] "2023-10-18T22:24:46-04:00"
##
## $results
## list_name display_name bestsellers_date published_date rank
## 1 Hardcover Fiction Hardcover Fiction 2023-10-14 2023-10-29 1
## 2 Hardcover Fiction Hardcover Fiction 2023-10-14 2023-10-29 2
## 3 Hardcover Fiction Hardcover Fiction 2023-10-14 2023-10-29 3
## 4 Hardcover Fiction Hardcover Fiction 2023-10-14 2023-10-29 4
## 5 Hardcover Fiction Hardcover Fiction 2023-10-14 2023-10-29 5
## 6 Hardcover Fiction Hardcover Fiction 2023-10-14 2023-10-29 6
## 7 Hardcover Fiction Hardcover Fiction 2023-10-14 2023-10-29 7
## 8 Hardcover Fiction Hardcover Fiction 2023-10-14 2023-10-29 8
## 9 Hardcover Fiction Hardcover Fiction 2023-10-14 2023-10-29 9
## 10 Hardcover Fiction Hardcover Fiction 2023-10-14 2023-10-29 10
## 11 Hardcover Fiction Hardcover Fiction 2023-10-14 2023-10-29 11
## 12 Hardcover Fiction Hardcover Fiction 2023-10-14 2023-10-29 12
## 13 Hardcover Fiction Hardcover Fiction 2023-10-14 2023-10-29 13
## 14 Hardcover Fiction Hardcover Fiction 2023-10-14 2023-10-29 14
## 15 Hardcover Fiction Hardcover Fiction 2023-10-14 2023-10-29 15
## rank_last_week weeks_on_list asterisk dagger
## 1 1 23 0 0
## 2 4 75 0 0
## 3 3 6 0 0
## 4 0 1 0 0
## 5 0 1 0 0
## 6 2 2 0 0
## 7 9 11 0 0
## 8 10 3 0 0
## 9 7 3 0 0
## 10 11 50 0 0
## 11 14 24 0 0
## 12 8 2 0 0
## 13 6 2 0 0
## 14 0 1 0 0
## 15 0 9 0 0
## amazon_product_url
## 1 https://www.amazon.com/dp/1649374046?tag=NYTBSREV-20
## 2 https://www.amazon.com/dp/038554734X?tag=NYTBSREV-20
## 3 https://www.amazon.com/dp/1668016133?tag=NYTBSREV-20
## 4 https://www.amazon.com/dp/0525619992?tag=NYTBSREV-20
## 5 https://www.amazon.com/dp/1501101811?tag=NYTBSREV-20
## 6 https://www.amazon.com/dp/0593542819?tag=NYTBSREV-20
## 7 https://www.amazon.com/dp/006332752X?tag=NYTBSREV-20
## 8 https://www.amazon.com/dp/0525954996?tag=NYTBSREV-20
## 9 https://www.amazon.com/dp/0316405698?tag=NYTBSREV-20
## 10 https://www.amazon.com/dp/0063251922?tag=NYTBSREV-20
## 11 https://www.amazon.com/dp/0802162177?tag=NYTBSREV-20
## 12 https://www.amazon.com/dp/1250799058?tag=NYTBSREV-20
## 13 https://www.amazon.com/dp/1984821954?tag=NYTBSREV-20
## 14 https://www.amazon.com/dp/1668009161?tag=NYTBSREV-20
## 15 https://www.amazon.com/dp/0593422945?tag=NYTBSREV-20
## isbns
## 1 1649374046, 1649374089, 1705085059, 1705085032, 9781649374042, 9781649374080, 9781705085059, 9781705085035
## 2 038554734X, 0593507533, 0385547374, 0593862406, 9780385547345, 9780593507537, 9780385547376, 9780593862407
## 3 1668016133, 1668014955, 1797161407, 9781668016138, 9781668014950, 9781797161402
## 4 0525619992, 0593600142, 0593724704, 0525620001, 9780525619994, 9780593600146, 9780593724705, 9780525620006
## 5 1501101811, 1501101838, 9781501101816, 9781501101830
## 6 0593542819, 0593542827, 9780593542811, 9780593542828
## 7 006332752X, 0063327546, 9780063327528, 9780063327542
## 8 0525954996, 059365532X, 9780525954996, 9780593655320
## 9 0316405698, 0316405795, 9780316405690, 9780316405799
## 10 0063251922, 006325199X, 0063251981, 9780063251922, 9780063251991, 9780063251984
## 11 0802162177, 1705070248, 1705070221, 9780802162175, 9781705070246, 9781705070222
## 12 1250799058, 1250323215, 1250799074, 9781250799050, 9781250323217, 9781250799074
## 13 1984821954, 1984821962, 9781984821959, 9781984821966
## 14 1668009161, 9781668009161
## 15 0593422945, 0593422961, 0593684125, 0593684141, 9780593422946, 9780593422960, 9780593684122, 9780593684146
## book_details
## 1 FOURTH WING, Violet Sorrengail is urged by the commanding general, who also is her mother, to become a candidate for the elite dragon riders., by Rebecca Yarros, Rebecca Yarros, , 0.00, , Red Tower, 9781649374042, 1649374046
## 2 LESSONS IN CHEMISTRY, A scientist and single mother living in California in the 1960s becomes a star on a TV cooking show., by Bonnie Garmus, Bonnie Garmus, , 0.00, , Doubleday, 9780385547345, 038554734X
## 3 HOLLY, The private detective Holly Gibney investigates whether a married pair of octogenarian academics had anything to do with Bonnie Dahl’s disappearance., by Stephen King, Stephen King, , 0.00, , Scribner, 9781668016138, 1668016133
## 4 SWORD CATCHER, An orphan who is the body double for a royal heir and a woman with magical abilities are drawn into the underworld of the city-state of Castellane., by Cassandra Clare, Cassandra Clare, , 0.00, , Del Rey, 9780525619994, 0525619992
## 5 BLOOD LINES, The second book in the Scott Brodie and Maggie Taylor series. After a mission in Venezuela, Brodie and Taylor search for the murderer of a fellow agent., by Nelson DeMille and Alex DeMille, Nelson DeMille and Alex DeMille, , 0.00, , Scribner, 9781501101816, 1501101811
## 6 JUDGMENT PREY, The 33rd book in the Prey series. Davenport and Flowers investigate the murder of a federal judge and his two young sons., by John Sandford, John Sandford, , 0.00, , Putnam, 9780593542811, 0593542819
## 7 TOM LAKE, Three daughters, who return to their family orchard in the spring of 2020, learn about their mother’s relationship with a famous actor., by Ann Patchett, Ann Patchett, , 0.00, , Harper, 9780063327528, 006332752X
## 8 THE ARMOR OF LIGHT, The fifth book in the Kingsbridge series. Change and turmoil affect various aspects of society in the latter part of the 18th century., by Ken Follett, Ken Follett, , 0.00, , Viking, 9780525954996, 0525954996
## 9 12 MONTHS TO LIVE, A criminal defense attorney who received a terminal diagnosis might be in danger of being murdered., by James Patterson and Mike Lupica, James Patterson and Mike Lupica, , 0.00, , Little, Brown, 9780316405690, 0316405698
## 10 DEMON COPPERHEAD, Winner of a 2023 Pulitzer Prize for fiction. A reimagining of Charles Dickens’s “David Copperfield” set in the mountains of southern Appalachia., by Barbara Kingsolver, Barbara Kingsolver, , 0.00, , Harper, 9780063251922, 0063251922
## 11 THE COVENANT OF WATER, Three generations of a family living on South India’s Malabar Coast suffer the loss of a family member by drowning., by Abraham Verghese, Abraham Verghese, , 0.00, , Grove, 9780802162175, 0802162177
## 12 STARLING HOUSE, An orphan hoping to improve things for her brother and herself goes inside a mansion connected to an author who disappeared over a century ago., by Alix E. Harrow, Alix E. Harrow, , 0.00, , Tor, 9781250799050, 1250799058
## 13 SECOND ACT, The former head of a Hollywood studio thinks the unfinished novel written by a woman he hired to get his affairs in order could be adapted for the screen., by Danielle Steel, Danielle Steel, , 0.00, , Delacorte, 9781984821959, 1984821954
## 14 BECOMING THE BOOGEYMAN, In a sequel to “Chasing the Boogeyman,” society’s true crime obsession is further explored using meta-fiction devices., by Richard Chizmar, Richard Chizmar, , 0.00, , Gallery, 9781668009161, 1668009161
## 15 THE HEAVEN & EARTH GROCERY STORE, Secrets held by the residents of a dilapidated neighborhood come to life when a skeleton is found at the bottom of a well., by James McBride, James McBride, , 0.00, , Riverhead, 9780593422946, 0593422945
## reviews
## 1 , , ,
## 2 , , ,
## 3 , , ,
## 4 , , ,
## 5 , , ,
## 6 , , ,
## 7 , , ,
## 8 , , ,
## 9 , , ,
## 10 , , ,
## 11 , , ,
## 12 , , ,
## 13 , , ,
## 14 , , ,
## 15 , , ,
top_sellers_list_df <- results$results
# It contains lists for 3 of it's columns, inside of them are dataframes for each book on the list
glimpse(top_sellers_list_df)
## Rows: 15
## Columns: 13
## $ list_name <chr> "Hardcover Fiction", "Hardcover Fiction", "Hardcove…
## $ display_name <chr> "Hardcover Fiction", "Hardcover Fiction", "Hardcove…
## $ bestsellers_date <chr> "2023-10-14", "2023-10-14", "2023-10-14", "2023-10-…
## $ published_date <chr> "2023-10-29", "2023-10-29", "2023-10-29", "2023-10-…
## $ rank <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
## $ rank_last_week <int> 1, 4, 3, 0, 0, 2, 9, 10, 7, 11, 14, 8, 6, 0, 0
## $ weeks_on_list <int> 23, 75, 6, 1, 1, 2, 11, 3, 3, 50, 24, 2, 2, 1, 9
## $ asterisk <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
## $ dagger <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
## $ amazon_product_url <chr> "https://www.amazon.com/dp/1649374046?tag=NYTBSREV-…
## $ isbns <list> [<data.frame[4 x 2]>], [<data.frame[4 x 2]>], [<dat…
## $ book_details <list> [<data.frame[1 x 10]>], [<data.frame[1 x 10]>], [<d…
## $ reviews <list> [<data.frame[1 x 4]>], [<data.frame[1 x 4]>], [<dat…
# We want the dataframes inside the book_details column
# Unfortunately the way the NY Times does it is making each data frame have only one row for each book
# There are 10 columns, the ones I'll choose to focus on are title, author, & publisher
str(top_sellers_list_df$book_details[[1]])
## 'data.frame': 1 obs. of 10 variables:
## $ title : chr "FOURTH WING"
## $ description : chr "Violet Sorrengail is urged by the commanding general, who also is her mother, to become a candidate for the eli"| __truncated__
## $ contributor : chr "by Rebecca Yarros"
## $ author : chr "Rebecca Yarros"
## $ contributor_note: chr ""
## $ price : chr "0.00"
## $ age_group : chr ""
## $ publisher : chr "Red Tower"
## $ primary_isbn13 : chr "9781649374042"
## $ primary_isbn10 : chr "1649374046"
Creating DataFrame of all the hardcover fiction books on the best sellers list
title <- c()
author <- c()
publisher <- c()
for (i in 1:15) {
cur <- top_sellers_list_df$book_details[[i]]
title[i] <- cur$title
author[i] <- cur$author
publisher[i] <- cur$publisher
}
top_15_hardcover_fiction_df <- data.frame(Title = title, Author = author, Publisher = publisher)
print(top_15_hardcover_fiction_df)
## Title Author
## 1 FOURTH WING Rebecca Yarros
## 2 LESSONS IN CHEMISTRY Bonnie Garmus
## 3 HOLLY Stephen King
## 4 SWORD CATCHER Cassandra Clare
## 5 BLOOD LINES Nelson DeMille and Alex DeMille
## 6 JUDGMENT PREY John Sandford
## 7 TOM LAKE Ann Patchett
## 8 THE ARMOR OF LIGHT Ken Follett
## 9 12 MONTHS TO LIVE James Patterson and Mike Lupica
## 10 DEMON COPPERHEAD Barbara Kingsolver
## 11 THE COVENANT OF WATER Abraham Verghese
## 12 STARLING HOUSE Alix E. Harrow
## 13 SECOND ACT Danielle Steel
## 14 BECOMING THE BOOGEYMAN Richard Chizmar
## 15 THE HEAVEN & EARTH GROCERY STORE James McBride
## Publisher
## 1 Red Tower
## 2 Doubleday
## 3 Scribner
## 4 Del Rey
## 5 Scribner
## 6 Putnam
## 7 Harper
## 8 Viking
## 9 Little, Brown
## 10 Harper
## 11 Grove
## 12 Tor
## 13 Delacorte
## 14 Gallery
## 15 Riverhead