The New York Times web site provides a rich set of APIs, as described here: https://developer.nytimes.com/apis You’ll need to start by signing up for an API key. Your task is to choose one of the New York Times APIs, construct an interface in R to read in the JSON data, and transform it into an R DataFrame.
#Load packages
library(httr2)
library(jsonlite)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ purrr::flatten() masks jsonlite::flatten()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(infer)
# The API call from NYTs requires API key to be put into the url at the end, and certain parameters for querying eg (lists/names)
# API call to find the list of best sellers that I want to research
api_key <- "HkhZiJEhn1oSfOE6SGZVqKWDyvYsCy7r"
url <- "https://api.nytimes.com/svc/books/v3/lists/names.json?api-key=HkhZiJEhn1oSfOE6SGZVqKWDyvYsCy7r"
#Using json to perform the api call
json_data <- fromJSON(url)
# Flatten the json data
flat_json <- flatten(json_data)
#Reading json data response into a data frame in R
df <- as.data.frame(flat_json$list_name_encoded)
head(df)
## flat_json$list_name_encoded
## 1 combined-print-and-e-book-fiction
## 2 combined-print-and-e-book-nonfiction
## 3 hardcover-fiction
## 4 hardcover-nonfiction
## 5 trade-fiction-paperback
## 6 mass-market-paperback
url2 <- "https://api.nytimes.com/svc/books/v3/lists/current/business-books.json?api-key=HkhZiJEhn1oSfOE6SGZVqKWDyvYsCy7r"
json_data2 <- fromJSON(url2)
# Flatten the json data
#Reading json data response into a data frame in R
df2 <- as.data.frame(json_data2$results$books)
head(df2)
## rank rank_last_week weeks_on_list asterisk dagger primary_isbn10
## 1 1 0 0 0 1 0735211299
## 2 2 0 0 0 0 0593652886
## 3 3 0 0 0 1 0593418573
## 4 4 0 0 0 0 1523505745
## 5 5 0 0 0 0 0593727932
## 6 6 0 0 0 1 1250183863
## primary_isbn13 publisher description price
## 1 9780735211292 Avery 0.00
## 2 9780593652886 Penguin Press 0.00
## 3 9780593418574 Optimism 0.00
## 4 9781523505746 Workman 0.00
## 5 9780593727935 Convergent 0.00
## 6 9781250183866 St. Martin's 0.00
## title author
## 1 ATOMIC HABITS James Clear
## 2 THE CREATIVE ACT Rick Rubin with Neil Strauss
## 3 UNREASONABLE HOSPITALITY Will Guidara
## 4 I WILL TEACH YOU TO BE RICH, SECOND EDITION Ramit Sethi
## 5 THE PLAN Kendra Adachi
## 6 EXTREME OWNERSHIP Jocko Willink and Leif Babin
## contributor contributor_note
## 1 by James Clear
## 2 by Rick Rubin with Neil Strauss
## 3 by Will Guidara
## 4 by Ramit Sethi
## 5 by Kendra Adachi
## 6 by Jocko Willink and Leif Babin
## book_image
## 1 https://storage.googleapis.com/du-prd/books/images/9780735211292.jpg
## 2 https://storage.googleapis.com/du-prd/books/images/9780593652886.jpg
## 3 https://storage.googleapis.com/du-prd/books/images/9780593418574.jpg
## 4 https://storage.googleapis.com/du-prd/books/images/9781523507870.jpg
## 5 https://storage.googleapis.com/du-prd/books/images/9780593727935.jpg
## 6 https://storage.googleapis.com/du-prd/books/images/9781466874961.jpg
## book_image_width book_image_height
## 1 328 495
## 2 341 500
## 3 331 500
## 4 329 495
## 5 331 500
## 6 330 495
## amazon_product_url
## 1 https://www.amazon.com/Atomic-Habits-Proven-Build-Break/dp/0735211299?tag=thenewyorktim-20
## 2 https://www.amazon.com/dp/0593652886?tag=thenewyorktim-20
## 3 https://www.amazon.com/dp/0593418573?tag=thenewyorktim-20
## 4 https://www.amazon.com/Will-Teach-You-Rich-Second-ebook/dp/B07GNXPP4P?tag=thenewyorktim-20
## 5 https://www.amazon.com/dp/0593727932?tag=thenewyorktim-20
## 6 http://www.amazon.com/Extreme-Ownership-U-S-Navy-SEALs-ebook/dp/B00VE4Y0Z2?tag=thenewyorktim-20
## age_group book_review_link first_chapter_link sunday_review_link
## 1
## 2
## 3
## 4
## 5
## 6
## article_chapter_link
## 1
## 2
## 3
## 4
## 5
## 6
## isbns
## 1 0735211299, 0735211302, 1847941834, 0593207092, 1804225789, 9780735211292, 9780735211308, 9781847941831, 9780593207093, 9781804225783
## 2 0593652886, 0593653424, 9780593652886, 9780593653425
## 3 0593418573, 0593418581, 9780593418574, 9780593418581
## 4 152350787X, 1523505745, 9781523507870, 9781523505746
## 5 0593727932, 0593727940, 9780593727935, 9780593727942
## 6 1250067057, 1466874961, 1427264295, 1427264309, 1250183863, 125018472X, 9781250067050, 9781466874961, 9781427264299, 9781427264305, 9781250183866, 9781250184726
## buy_links
## 1 Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/Atomic-Habits-Proven-Build-Break/dp/0735211299?tag=thenewyorktim-20, https://goto.applebooks.apple/9780735211292?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9780735211292, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FATOMIC%2BHABITS%2FJames%2BClear%2F9780735211292, https://bookshop.org/a/3546/9780735211292
## 2 Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/dp/0593652886?tag=thenewyorktim-20, https://goto.applebooks.apple/9780593652886?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9780593652886, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FTHE%2BCREATIVE%2BACT%2FRick%2BRubin%2Bwith%2BNeil%2BStrauss%2F9780593652886, https://bookshop.org/a/3546/9780593652886
## 3 Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/dp/0593418573?tag=thenewyorktim-20, https://goto.applebooks.apple/9780593418574?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9780593418574, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FUNREASONABLE%2BHOSPITALITY%2FWill%2BGuidara%2F9780593418574, https://bookshop.org/a/3546/9780593418574
## 4 Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/Will-Teach-You-Rich-Second-ebook/dp/B07GNXPP4P?tag=thenewyorktim-20, https://goto.applebooks.apple/9781523505746?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9781523505746, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FI%2BWILL%2BTEACH%2BYOU%2BTO%2BBE%2BRICH%252C%2BSECOND%2BEDITION%2FRamit%2BSethi%2F9781523505746, https://bookshop.org/a/3546/9781523505746
## 5 Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/dp/0593727932?tag=thenewyorktim-20, https://goto.applebooks.apple/9780593727935?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9780593727935, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FTHE%2BPLAN%2FKendra%2BAdachi%2F9780593727935, https://bookshop.org/a/3546/9780593727935
## 6 Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, http://www.amazon.com/Extreme-Ownership-U-S-Navy-SEALs-ebook/dp/B00VE4Y0Z2?tag=thenewyorktim-20, https://goto.applebooks.apple/9781250183866?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9781250183866, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FEXTREME%2BOWNERSHIP%2FJocko%2BWillink%2Band%2BLeif%2BBabin%2F9781250183866, https://bookshop.org/a/3546/9781250183866
## book_uri
## 1 nyt://book/0398a355-c032-534e-a0af-647b06f0840d
## 2 nyt://book/da28ca16-1ebd-55aa-aba3-fde278604a82
## 3 nyt://book/bc1ec950-e54a-57a5-a404-c9007374b508
## 4 nyt://book/b0fe54f6-2468-55d3-bedf-0338f98478e6
## 5 nyt://book/f47212ae-5807-50f8-b20b-a532ef80a5bb
## 6 nyt://book/b0cec2f9-300f-5850-8c43-8a67152a96a5
I want to test if the top 5 books have a larger image size than the bottom 5 from the top 10 best selling buisness books.
df2 <- df2[,c(1,11,16,17)]
df2 <- df2 %>%
mutate(image_area = book_image_width * book_image_height) %>%
mutate(top.5 = rank < 6)
ggplot(df2, aes(x = top.5, y = image_area)) +
geom_boxplot()
df2 %>%
group_by(top.5) %>%
summarise(mean_area = mean(image_area))
## # A tibble: 2 × 2
## top.5 mean_area
## <lgl> <dbl>
## 1 FALSE 162423
## 2 TRUE 165343
obs_diff <- df2 %>%
specify(image_area ~ top.5) %>%
calculate(stat = "diff in means", order = c("TRUE", "FALSE"))
t.test(image_area ~ top.5, data = df2)
##
## Welch Two Sample t-test
##
## data: image_area by top.5
## t = -1.608, df = 7.4728, p-value = 0.1492
## alternative hypothesis: true difference in means between group FALSE and group TRUE is not equal to 0
## 95 percent confidence interval:
## -7159.403 1319.403
## sample estimates:
## mean in group FALSE mean in group TRUE
## 162423 165343
Using the t.test function we find that we fail to reject the null hypothesis, the 95% confidence interval contains 0, and with a p value of 0.1492, our difference in means is too small, given if we want a 95% confidence level.