Week9

Author

Zihao Yu


1.How will I tackle the problem?

I will start by studying the code examples in the link to learn how to call APIs in R. Then, I will follow the assignment instructions to retrieve, clean, and analyze the data. Finally, I will organize the results as required for the assignment.

Question: What are the top five books on The New York Times fiction bestseller list?

2.What data challenges do I anticipate?

As I learn and code, there will be some parts where I don’t quite understand the details, and I may need more time to grasp them.


3. Store key in an environment variable

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.1     ✔ stringr   1.5.2
✔ ggplot2   4.0.2     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── 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(httr2)

api_key <- Sys.getenv("NYT_API_KEY")
req_from_nyt <-
 request("https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json") |>
  req_url_query(`api-key` = api_key) |>
  req_perform()

seeing the info.

json_data <- resp_body_json(req_from_nyt)

names(json_data$results)
 [1] "display_name"            "list_name"              
 [3] "list_name_encoded"       "previous_published_date"
 [5] "published_date"          "bestsellers_date"       
 [7] "normal_list_ends_at"     "updated"                
 [9] "list_id"                 "uri"                    
[11] "books"                   "corrections"            
length(json_data$results$books)
[1] 15
names(json_data$results$books[[1]])
 [1] "age_group"            "amazon_product_url"   "article_chapter_link"
 [4] "asterisk"             "author"               "book_image"          
 [7] "book_image_height"    "book_image_width"     "book_review_link"    
[10] "book_uri"             "contributor"          "contributor_note"    
[13] "created_date"         "dagger"               "description"         
[16] "first_chapter_link"   "price"                "primary_isbn10"      
[19] "primary_isbn13"       "publisher"            "rank"                
[22] "rank_last_week"       "sunday_review_link"   "title"               
[25] "updated_date"         "weeks_on_list"        "isbns"               
[28] "buy_links"           

fivce columns–(author, publisher, rank, title, weeks_on_list).

library(purrr)

books_info <- 
  tibble(
    title = map_chr(json_data$results$books, "title"),
    author = map_chr(json_data$results$books, "author"),
    publisher = map_chr(json_data$results$books, "publisher"),
    rank = map_int(json_data$results$books, "rank"),
    weeks_on_list = map_int(json_data$results$books, "weeks_on_list")
)

books_info
# A tibble: 15 × 5
   title                            author         publisher  rank weeks_on_list
   <chr>                            <chr>          <chr>     <int>         <int>
 1 JUDGE STONE                      Viola Davis a… Little, …     1             2
 2 THE CORRESPONDENT                Virginia Evans Crown         2            21
 3 BLOODLUST                        Sandra Brown   Grand Ce…     3             1
 4 MY HUSBAND'S WIFE                Alice Feeney   Pine & C…     4             8
 5 INNAMORATA                       Ava Reid       Del Rey       5             1
 6 CARL'S DOOMSDAY SCENARIO         Matt Dinniman  Ace           6            10
 7 MOTHER OF DEATH AND DAWN         Carissa Broad… Bramble       7             1
 8 KIN                              Tayari Jones   Knopf         8             4
 9 BETWEEN TWO FIRES                Christopher B… Tor Nigh…     9             3
10 THE DUNGEON ANARCHIST'S COOKBOOK Matt Dinniman  Ace          10             5
11 THE WIDOW                        John Grisham   Doubleday    11            22
12 ALCHEMISED                       SenLinYu       Del Rey      12            26
13 THE CROSSROADS                   C.J. Box       Putnam       13             4
14 THE GATE OF THE FERAL GODS       Matt Dinniman  Ace          14             2
15 DUNGEON CRAWLER CARL             Matt Dinniman  Ace          15            12
Top_books <-
  books_info |> 
  arrange(desc(weeks_on_list)) |> 
  select(title, author, rank, weeks_on_list)

Top_books
# A tibble: 15 × 4
   title                            author                    rank weeks_on_list
   <chr>                            <chr>                    <int>         <int>
 1 ALCHEMISED                       SenLinYu                    12            26
 2 THE WIDOW                        John Grisham                11            22
 3 THE CORRESPONDENT                Virginia Evans               2            21
 4 DUNGEON CRAWLER CARL             Matt Dinniman               15            12
 5 CARL'S DOOMSDAY SCENARIO         Matt Dinniman                6            10
 6 MY HUSBAND'S WIFE                Alice Feeney                 4             8
 7 THE DUNGEON ANARCHIST'S COOKBOOK Matt Dinniman               10             5
 8 KIN                              Tayari Jones                 8             4
 9 THE CROSSROADS                   C.J. Box                    13             4
10 BETWEEN TWO FIRES                Christopher Buehlman         9             3
11 JUDGE STONE                      Viola Davis and James P…     1             2
12 THE GATE OF THE FERAL GODS       Matt Dinniman               14             2
13 BLOODLUST                        Sandra Brown                 3             1
14 INNAMORATA                       Ava Reid                     5             1
15 MOTHER OF DEATH AND DAWN         Carissa Broadbent            7             1
sum(map_int(json_data$results$books, "weeks_on_list"))
[1] 122

Conclusion.

(1)The five books with the longest cumulative time on the chart.

The current Fiction chart, sorted by “weeks_on_list” from highest to lowest, is as follows: “ALCHEMISED,” “THE WIDOW,” “THE CORRESPONDENT,” “DUNGEON CRAWLER CARL,” and “CARL’S DOOMSDAY SCENARIO.” These five books are the fiction on the current chart that have been on the list the longest and have maintained the most stable rankings.


Top_books_by_rank <-
  books_info |> 
  arrange(rank) |> 
  select(title, author, rank, weeks_on_list)

Top_books_by_rank
# A tibble: 15 × 4
   title                            author                    rank weeks_on_list
   <chr>                            <chr>                    <int>         <int>
 1 JUDGE STONE                      Viola Davis and James P…     1             2
 2 THE CORRESPONDENT                Virginia Evans               2            21
 3 BLOODLUST                        Sandra Brown                 3             1
 4 MY HUSBAND'S WIFE                Alice Feeney                 4             8
 5 INNAMORATA                       Ava Reid                     5             1
 6 CARL'S DOOMSDAY SCENARIO         Matt Dinniman                6            10
 7 MOTHER OF DEATH AND DAWN         Carissa Broadbent            7             1
 8 KIN                              Tayari Jones                 8             4
 9 BETWEEN TWO FIRES                Christopher Buehlman         9             3
10 THE DUNGEON ANARCHIST'S COOKBOOK Matt Dinniman               10             5
11 THE WIDOW                        John Grisham                11            22
12 ALCHEMISED                       SenLinYu                    12            26
13 THE CROSSROADS                   C.J. Box                    13             4
14 THE GATE OF THE FERAL GODS       Matt Dinniman               14             2
15 DUNGEON CRAWLER CARL             Matt Dinniman               15            12

(2)The top five books on the current

The current Fiction chart, sorted by rank, lists the top five as follows: ‘JUDGE STONE’, ‘THE CORRESPONDENT’, ‘BLOODLUST’, ‘MY HUSBAND’S WIFE’, and ‘INNAMORATA’. These are the five most popular fiction on the list.

“THE CORRESPONDENT” ranks second, having appeared on the list 21 times—just five fewer than the top one by ‘weeks_on_list’. Overall, this is the most popular fiction.


Additional information

2. I sent a request to the endpoint “/svc/books/v3/lists/current/hardcover-fiction.json” to retrieve the data for the current Hardcover Fiction list, and used the ‘api-key’ parameter in the request to authenticate.

3. I extracted data from ‘json\(results\)books’ in the return results and organized it into a tidy data frame. I retained the ‘title’, ‘author’, ‘publisher’, ‘rank’, and `weeks_on_list’ columns. There are no missing values in the data.