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.
The following libraries are required for running this code
I decided to use the NY Times books API, which gives the user data on book rankings by category or what they call list, for a given week. It’s the data that comprises makes up their Best Seller List.
First we want to construct the URL so we pull the data we want with the parameters we want.
The parameters include date, list, and api_key. For the purposes of this assignment I used the last day before the COVID-19 lockdown in New York, the list of books was hardcover-nonfiction and my api-key.
For the list parameter you could choose from the following options:
|
|
When we first read the raw.data, we get some information about the link as well as the link results. From the results list we can grab the books data frame.
Finally we can simplify the book data frame that the API gives us to a data frame with the information we want in it.
book.rank.df <-
book.data[,c("rank",
"rank_last_week",
"weeks_on_list",
"title",
"author",
"description",
"primary_isbn13")]The final result
| rank | rank_last_week | weeks_on_list | title | author | description | primary_isbn13 |
|---|---|---|---|---|---|---|
| 1 | 0 | 1 | THE SPLENDID AND THE VILE | Erik Larson | An examination of the leadership of the prime minister Winston Churchill. | 9780385348713 |
| 2 | 1 | 11 | THE MAMBA MENTALITY | Kobe Bryant | Various skills and techniques used on the court by the late Los Angeles Lakers player. | 9780374201234 |
| 3 | 0 | 1 | UNKNOWN VALOR | Martha MacCallum | The Fox News anchor weaves stories of combat veterans who fought during World War II. | 9780062853851 |
| 4 | 0 | 1 | THE HOPE OF GLORY | Jon Meacham | The Pulitzer Prize-winning biographer delves into the seven last sayings of Jesus as recorded in the Gospels. | 9780593236666 |
| 5 | 3 | 4 | OPEN BOOK | Jessica Simpson with Kevin Carr O’Leary | The singer, actress and fashion designer discloses times of success, trauma and addiction. | 9780062899965 |