Our task for the week 9 assignment is to choose one of the New York Times APIs, construct an interface in R to read in the JSON data, and transform it to an R dataframe.
For this assignment I opted for the Best Seller Books API from New York Times.
Lets load the NYT API and parse it R. For parsing json api, I am using JSONLITE Library.
json_file <- "https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json?api-key=5fe82da6fd5740a7a90d749aa086a9b5"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))
Create Data frame for the data retreived in the above step. Please not that I am using list() function of DPLYR package to append data when parsed through FOR Loop.
listTab <- list()
rankTab <- list()
dataBestSeller <- length(json_data$results)
rankData <- length(json_data$results)
for (i in 1:length(json_data$results)) {
rankData$Title <- dataBestSeller$Title <- gsub( "#", "", json_data$results$title[i])
dataBestSeller$Description <- json_data$results$description[i]
dataBestSeller$Contributor <- json_data$results$contributor[i]
dataBestSeller$Author <- json_data$results$author[i]
dataBestSeller$Publisher <- json_data$results$publisher[i]
dataBestSeller$isbn10 <- json_data$results$isbns[[i]]$isbn10
# rankData$Title <- gsub( "#", "", json_data$results$title[i])
rankData$Rank <- json_data$results$ranks_history[[i]]$rank
rankData$Weeks_On_List <- json_data$results$ranks_history[[i]]$weeks_on_list
rankData$ISBN10 <- json_data$results$ranks_history[[i]]$primary_isbn10
rankData$ListName <- json_data$results$ranks_history[[i]]$list_name
rankData$Publication_Date <- json_data$results$ranks_history[[i]]$published_date
rankData$Best_Seller_Date <- json_data$results$ranks_history[[i]]$bestsellers_date
dfBooks <- data.frame(dataBestSeller)
rankBooks <- data.frame(rankData)
listTab[[i]] <- dfBooks
rankTab[[i]] <- rankBooks
}
opBooks <- bind_rows(listTab)
opBooks$X11L <- NULL
opRank <- bind_rows(rankTab)
opRank$X11L <- NULL
kable(opBooks)
Title | Description | Contributor | Author | Publisher | isbn10 |
---|---|---|---|---|---|
“I GIVE YOU MY BODY …” | The author of the Outlander novels gives tips on writing sex scenes, drawing on examples from the books. | by Diana Gabaldon | Diana Gabaldon | Dell | 0399178570 |
“MOST BLESSED OF THE PATRIARCHS” | A character study that attempts to make sense of Jefferson’s contradictions. | by Annette Gordon-Reed and Peter S. Onuf | Annette Gordon-Reed and Peter S Onuf | Liveright | 0871404427 |
ASKGARYVEE | The entrepreneur expands on subjects addressed on his Internet show, like marketing, management and social media. | by Gary Vaynerchuk | Gary Vaynerchuk | HarperCollins | 0062273124 |
ASKGARYVEE | The entrepreneur expands on subjects addressed on his Internet show, like marketing, management and social media. | by Gary Vaynerchuk | Gary Vaynerchuk | HarperCollins | 0062273132 |
GIRLBOSS | An online fashion retailer traces her path to success. | by Sophia Amoruso | Sophia Amoruso | Portfolio/Penguin/Putnam | 039916927X |
GIRLBOSS | An online fashion retailer traces her path to success. | by Sophia Amoruso | Sophia Amoruso | Portfolio/Penguin/Putnam | 1591847931 |
$100 STARTUP | How to build a profitable start up for $100 or less and be your own boss. | by Chris Guillebeau | Chris Guillebeau | Crown Business | 0307951529 |
$20 PER GALLON | by Christopher Steiner | Christopher Steiner | Grand Central | NA | |
’57, Chicago | NA | NA | Steve Monroe | NA | 0786867302 |
‘ROCK OF AGES:’‘ROLLING STONE’‘HISTORY OF ROCK AND ROLL’ | NA | NA | GEOFFREY STOKES, KEN TUCKER’ ’ED WARD | NA | 0671630687 |
‘THE HIGH ROAD TO CHINA: GEORGE BOGLE, THE PANCHEN LAMA AND THE FIRST BRITISH EXPEDITION TO TIBET’ | NA | NA | KATE TELTSCHER | NA | 0374217009 |
’TIL DEATH | by Sharon Sala | Sharon Sala | Harlequin Mira | 0778314278 | |
’TIL DEATH DO US PART | A matchmaker in Victorian England turns to a crime novelist for help when she starts receiving a series of disturbingly personalized trinkets. | by Amanda Quick | Amanda Quick | Berkley | 069819361X |
’TIL DEATH DO US PART | A matchmaker in Victorian England turns to a crime novelist for help when she starts receiving a series of disturbingly personalized trinkets. | by Amanda Quick | Amanda Quick | Berkley | 039917446X |
# kable(opRank)