I’ll be signing up for New York Times Developer to obtain an API key and then choose an API from the select list of available APIs. From the select list, I will use the Books API for this assignment. More specifically from the list, I’ll be using the best sellers path.
api_key <- "aEZ7qNSPXDAeQlTcdnNvTGSyM4lxoIDa"
base_url <- "https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json"
In order to construct my request via using the httr package due to its GET() function which further retrieves the query list of choosing from the API as well as specific functions to help with formatting once retrieved.
response <- GET(
url = base_url,
query = list(
"api-key" = api_key,
"author" = "George RR Martin"
)
)
{
content_text <- content(response, as = "text", encoding = "UTF-8")
content_json <- fromJSON(content_text, flatten = TRUE)
str(content_json)
df <- as.data.frame(content_json$results)
view(df)
}
## List of 4
## $ status : chr "OK"
## $ copyright : chr "Copyright (c) 2025 The New York Times Company. All Rights Reserved."
## $ num_results: int 6
## $ results :'data.frame': 6 obs. of 11 variables:
## ..$ title : chr [1:6] "A CLASH OF KINGS" "A FEAST FOR CROWS" "A GAME OF THRONES" "A GAME OF THRONES" ...
## ..$ description : chr [1:6] "The second book in the Song of Ice and Fire series. Read by Roy Dotrice. 37 hours, 12 minutes unabridged." "The fourth book in the Song of Ice and Fire series. Read by Roy Dotrice. 33 hours, 51 minutes unabridged." "In the frozen wastes to the north of Winterfell, sinister and supernatural forces are mustering." "In the frozen wastes to the north of Winterfell, sinister and supernatural forces are mustering. Read by Roy Do"| __truncated__ ...
## ..$ contributor : chr [1:6] "by George R.R. Martin" "by George R.R. Martin" "by George R.R. Martin" "by George R.R. Martin" ...
## ..$ author : chr [1:6] "George RR Martin" "George RR Martin" "George RR Martin" "George RR Martin" ...
## ..$ contributor_note: chr [1:6] "" "" "" "" ...
## ..$ price : chr [1:6] "0.00" "0.00" "0.00" "0.00" ...
## ..$ age_group : chr [1:6] "" "" "" "" ...
## ..$ publisher : chr [1:6] "Random House Audio" "Random House Audio" "Bantam" "Random House Audio" ...
## ..$ isbns :List of 6
## .. ..$ :'data.frame': 9 obs. of 2 variables:
## .. .. ..$ isbn10: chr [1:9] "0553579908" "0553897853" "0553381695" "0553108034" ...
## .. .. ..$ isbn13: chr [1:9] "9780553579901" "9780553897852" "9780553381696" "9780553108033" ...
## .. ..$ :'data.frame': 5 obs. of 2 variables:
## .. .. ..$ isbn10: chr [1:5] "055358202X" "0553900323" "0553582038" "0553801503" ...
## .. .. ..$ isbn13: chr [1:5] "9780553582024" "9780553900323" "9780553582031" "9780553801507" ...
## .. ..$ :'data.frame': 9 obs. of 2 variables:
## .. .. ..$ isbn10: chr [1:9] "0553573403" "0553386794" "0553381687" "0553593714" ...
## .. .. ..$ isbn13: chr [1:9] "9780553573404" "9780553386790" "9780553381689" "9780553593716" ...
## .. ..$ :'data.frame': 1 obs. of 2 variables:
## .. .. ..$ isbn10: chr "0307913090"
## .. .. ..$ isbn13: chr "9780307913098"
## .. ..$ :'data.frame': 5 obs. of 2 variables:
## .. .. ..$ isbn10: chr [1:5] "055357342X" "055389787X" "0553381709" "0553106635" ...
## .. .. ..$ isbn13: chr [1:5] "9780553573428" "9780553897876" "9780553381702" "9780553106633" ...
## .. ..$ :'data.frame': 1 obs. of 2 variables:
## .. .. ..$ isbn10: chr "1477849106"
## .. .. ..$ isbn13: chr "9781477849101"
## ..$ ranks_history :List of 6
## .. ..$ :'data.frame': 3 obs. of 11 variables:
## .. .. ..$ primary_isbn10 : chr [1:3] "" "" ""
## .. .. ..$ primary_isbn13 : chr [1:3] "9781415902004" "9781415902004" "9781415902004"
## .. .. ..$ rank : int [1:3] 6 4 10
## .. .. ..$ list_name : chr [1:3] "Audio Fiction" "Audio Fiction" "Audio Fiction"
## .. .. ..$ display_name : chr [1:3] "Audio Fiction" "Audio Fiction" "Audio Fiction"
## .. .. ..$ published_date : chr [1:3] "2019-07-14" "2019-06-16" "2019-05-12"
## .. .. ..$ bestsellers_date: chr [1:3] "2019-06-29" "2019-06-01" "2019-04-27"
## .. .. ..$ weeks_on_list : int [1:3] 0 0 0
## .. .. ..$ rank_last_week : int [1:3] 4 10 0
## .. .. ..$ asterisk : int [1:3] 0 0 0
## .. .. ..$ dagger : int [1:3] 0 0 0
## .. ..$ :'data.frame': 1 obs. of 11 variables:
## .. .. ..$ primary_isbn10 : chr ""
## .. .. ..$ primary_isbn13 : chr "9780449011881"
## .. .. ..$ rank : int 13
## .. .. ..$ list_name : chr "Audio Fiction"
## .. .. ..$ display_name : chr "Audio Fiction"
## .. .. ..$ published_date : chr "2019-06-16"
## .. .. ..$ bestsellers_date: chr "2019-06-01"
## .. .. ..$ weeks_on_list : int 0
## .. .. ..$ rank_last_week : int 0
## .. .. ..$ asterisk : int 0
## .. .. ..$ dagger : int 0
## .. ..$ :'data.frame': 9 obs. of 11 variables:
## .. .. ..$ primary_isbn10 : chr [1:9] "0553897845" "0553381687" "0553381687" "0553897845" ...
## .. .. ..$ primary_isbn13 : chr [1:9] "9780553897845" "9780553381689" "9780553381689" "9780553897845" ...
## .. .. ..$ rank : int [1:9] 4 14 8 2 7 12 8 9 5
## .. .. ..$ list_name : chr [1:9] "Combined Print and E-Book Fiction" "Trade Fiction Paperback" "Trade Fiction Paperback" "Combined Print and E-Book Fiction" ...
## .. .. ..$ display_name : chr [1:9] "Combined Print & E-Book Fiction" "Paperback Trade Fiction" "Paperback Trade Fiction" "Combined Print & E-Book Fiction" ...
## .. .. ..$ published_date : chr [1:9] "2019-06-16" "2019-06-16" "2019-06-09" "2019-06-09" ...
## .. .. ..$ bestsellers_date: chr [1:9] "2019-06-01" "2019-06-01" "2019-05-25" "2019-05-25" ...
## .. .. ..$ weeks_on_list : int [1:9] 58 69 68 57 56 55 54 53 52
## .. .. ..$ rank_last_week : int [1:9] 2 8 0 7 12 8 9 5 0
## .. .. ..$ asterisk : int [1:9] 0 0 0 0 0 0 0 0 0
## .. .. ..$ dagger : int [1:9] 0 0 0 0 0 0 0 0 0
## .. ..$ :'data.frame': 4 obs. of 11 variables:
## .. .. ..$ primary_isbn10 : chr [1:4] "" "" "" ""
## .. .. ..$ primary_isbn13 : chr [1:4] "9781415900642" "9781415900642" "9781415900642" "9781415900642"
## .. .. ..$ rank : int [1:4] 7 2 3 7
## .. .. ..$ list_name : chr [1:4] "Audio Fiction" "Audio Fiction" "Audio Fiction" "Audio Fiction"
## .. .. ..$ display_name : chr [1:4] "Audio Fiction" "Audio Fiction" "Audio Fiction" "Audio Fiction"
## .. .. ..$ published_date : chr [1:4] "2019-07-14" "2019-06-16" "2019-05-12" "2019-04-14"
## .. .. ..$ bestsellers_date: chr [1:4] "2019-06-29" "2019-06-01" "2019-04-27" "2019-03-30"
## .. .. ..$ weeks_on_list : int [1:4] 0 0 0 0
## .. .. ..$ rank_last_week : int [1:4] 2 3 7 10
## .. .. ..$ asterisk : int [1:4] 0 0 0 0
## .. .. ..$ dagger : int [1:4] 0 0 0 0
## .. ..$ :'data.frame': 3 obs. of 11 variables:
## .. .. ..$ primary_isbn10 : chr [1:3] "" "" ""
## .. .. ..$ primary_isbn13 : chr [1:3] "9780739308738" "9780739308738" "9780739308738"
## .. .. ..$ rank : int [1:3] 12 6 15
## .. .. ..$ list_name : chr [1:3] "Audio Fiction" "Audio Fiction" "Audio Fiction"
## .. .. ..$ display_name : chr [1:3] "Audio Fiction" "Audio Fiction" "Audio Fiction"
## .. .. ..$ published_date : chr [1:3] "2019-07-14" "2019-06-16" "2019-05-12"
## .. .. ..$ bestsellers_date: chr [1:3] "2019-06-29" "2019-06-01" "2019-04-27"
## .. .. ..$ weeks_on_list : int [1:3] 0 0 0
## .. .. ..$ rank_last_week : int [1:3] 6 15 0
## .. .. ..$ asterisk : int [1:3] 0 0 0
## .. .. ..$ dagger : int [1:3] 0 0 0
## .. ..$ :'data.frame': 0 obs. of 0 variables
## ..$ reviews :List of 6
## .. ..$ :'data.frame': 1 obs. of 4 variables:
## .. .. ..$ book_review_link : chr ""
## .. .. ..$ first_chapter_link : chr ""
## .. .. ..$ sunday_review_link : chr ""
## .. .. ..$ article_chapter_link: chr ""
## .. ..$ :'data.frame': 1 obs. of 4 variables:
## .. .. ..$ book_review_link : chr ""
## .. .. ..$ first_chapter_link : chr ""
## .. .. ..$ sunday_review_link : chr ""
## .. .. ..$ article_chapter_link: chr ""
## .. ..$ :'data.frame': 1 obs. of 4 variables:
## .. .. ..$ book_review_link : chr ""
## .. .. ..$ first_chapter_link : chr ""
## .. .. ..$ sunday_review_link : chr ""
## .. .. ..$ article_chapter_link: chr "https://www.nytimes.com/2011/04/10/arts/television/game-of-thrones-on-hbo-from-george-r-r-martin-novels.html"
## .. ..$ :'data.frame': 1 obs. of 4 variables:
## .. .. ..$ book_review_link : chr ""
## .. .. ..$ first_chapter_link : chr ""
## .. .. ..$ sunday_review_link : chr ""
## .. .. ..$ article_chapter_link: chr ""
## .. ..$ :'data.frame': 1 obs. of 4 variables:
## .. .. ..$ book_review_link : chr ""
## .. .. ..$ first_chapter_link : chr ""
## .. .. ..$ sunday_review_link : chr ""
## .. .. ..$ article_chapter_link: chr ""
## .. ..$ :'data.frame': 1 obs. of 4 variables:
## .. .. ..$ book_review_link : chr ""
## .. .. ..$ first_chapter_link : chr ""
## .. .. ..$ sunday_review_link : chr ""
## .. .. ..$ article_chapter_link: chr ""
According to the previous dataframe , there are 3 nested columns; isbns, ranks_history, reviews that contain <dataframe[y x y]> so therefore I explore the data further by unnesting those columns using the unnest function. This dataframe is much larger because of these expansions and upon closer inspection shows that the data holds some repeated values through the same book such as publishers and primary isbns, however this is showing off the different dates of the New York Times Best Sellers date, important for recording the history of this awards among these kinds of books, as the path to this API suggests.
df_unnest <- df %>%
unnest(cols = isbns) %>%
unnest(cols = ranks_history) %>%
unnest(cols = reviews)
glimpse(df_unnest)
## Rows: 132
## Columns: 25
## $ title <chr> "A CLASH OF KINGS", "A CLASH OF KINGS", "A CLASH …
## $ description <chr> "The second book in the Song of Ice and Fire seri…
## $ contributor <chr> "by George R.R. Martin", "by George R.R. Martin",…
## $ author <chr> "George RR Martin", "George RR Martin", "George R…
## $ contributor_note <chr> "", "", "", "", "", "", "", "", "", "", "", "", "…
## $ price <chr> "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "…
## $ age_group <chr> "", "", "", "", "", "", "", "", "", "", "", "", "…
## $ publisher <chr> "Random House Audio", "Random House Audio", "Rand…
## $ isbn10 <chr> "0553579908", "0553579908", "0553579908", "055389…
## $ isbn13 <chr> "9780553579901", "9780553579901", "9780553579901"…
## $ primary_isbn10 <chr> "", "", "", "", "", "", "", "", "", "", "", "", "…
## $ primary_isbn13 <chr> "9781415902004", "9781415902004", "9781415902004"…
## $ rank <int> 6, 4, 10, 6, 4, 10, 6, 4, 10, 6, 4, 10, 6, 4, 10,…
## $ list_name <chr> "Audio Fiction", "Audio Fiction", "Audio Fiction"…
## $ display_name <chr> "Audio Fiction", "Audio Fiction", "Audio Fiction"…
## $ published_date <chr> "2019-07-14", "2019-06-16", "2019-05-12", "2019-0…
## $ bestsellers_date <chr> "2019-06-29", "2019-06-01", "2019-04-27", "2019-0…
## $ weeks_on_list <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ rank_last_week <int> 4, 10, 0, 4, 10, 0, 4, 10, 0, 4, 10, 0, 4, 10, 0,…
## $ asterisk <int> 0, 0, 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, 0, 0…
## $ book_review_link <chr> "", "", "", "", "", "", "", "", "", "", "", "", "…
## $ first_chapter_link <chr> "", "", "", "", "", "", "", "", "", "", "", "", "…
## $ sunday_review_link <chr> "", "", "", "", "", "", "", "", "", "", "", "", "…
## $ article_chapter_link <chr> "", "", "", "", "", "", "", "", "", "", "", "", "…
In further transforming this code into the R dataframe, I aim to group this unnested data via the title of the books that made it onto the bestsellers list assuming they have bestseller dates in the dataframe nyt_ranks.
nyt_ranks <- df_unnest %>%
group_by(title) %>%
summarize(
min_rank = min(rank),
max_rank = max(rank),
first_date = min(bestsellers_date),
last_date = max(bestsellers_date),
.groups = "drop"
)
head(nyt_ranks)
## # A tibble: 4 × 5
## title min_rank max_rank first_date last_date
## <chr> <int> <int> <chr> <chr>
## 1 A CLASH OF KINGS 4 10 2019-04-27 2019-06-29
## 2 A FEAST FOR CROWS 13 13 2019-06-01 2019-06-01
## 3 A GAME OF THRONES 2 14 2019-03-30 2019-06-29
## 4 A STORM OF SWORDS 6 15 2019-04-27 2019-06-29
Throughout this assignment, I’ve demonstrated my awareness to signing up for the API key for my own access, choosing the Books API (along with its New York Times Best Seller path) reading that API and its path into my dataframes in R. Afterwards, I began work on constructing an interface with R to read its JSON data and then transforming the data to what I see as more visible and on topic for the history of George RR Martin books that have been on the New York Times Best Seller list and when.