The New York Times web site provides a rich set of APIs, as described here: http://developer.nytimes.com/docs 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 to an R dataframe.
library(jsonlite)
library(DT)
library(tidyr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
key <- "&api-key=866c0b4f1a5c4bb4a978ed35735f4ccd"
url <- "https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json?age-group=10"
maindata <- fromJSON(paste0(url, key))
head(maindata)
## $status
## [1] "OK"
##
## $copyright
## [1] "Copyright (c) 2018 The New York Times Company. All Rights Reserved."
##
## $num_results
## [1] 73
##
## $results
## title
## 1 14 COWS FOR AMERICA
## 2 A HERO FOR WONDLA
## 3 A TALE DARK AND GRIMM
## 4 AMELIA BEDELIA MEANS BUSINESS
## 5 AMELIA BEDELIA UNLEASHED
## 6 ASCENDANCE TRILOGY
## 7 BARACK OBAMA: SON OF PROMISE, CHILD OF HOPE
## 8 BATTLE BUNNY
## 9 BOMB
## 10 BOOKS OF EMBER
## 11 CHOMP
## 12 CIRQUE DU FREAK
## 13 DEAD END IN NORVELT
## 14 DESCENDANTS
## 15 FANTASY LEAGUE
## 16 FAST BREAK
## 17 FUZZY MUD
## 18 GEORGE WASHINGTON
## 19 GUS & ME
## 20 HERO
## description
## 1 Masai villagers in Kenya seek to comfort Americans after 9/11.
## 2 Something sinister threatens planet Orbona.
## 3 Hansel and Gretel in other Grimm tales.
## 4 She wants a bike.
## 5 Shes getting a puppy.
## 6 The kingdom of Carthya is on the brink of disaster.
## 7 Yes, he can; a childrens biography of the president-elect.
## 8 With pencil edits, Birthday Bunny is transformed into a combat master.
## 9 The cross-continental race to build the atomic bomb.
## 10 In a postapocalyptic future, children try to save the world.
## 11 Life gets wilder for an animal wrangler's son.
## 12 A vampires assistant navigates blood and gore.
## 13 Typing obituaries for a neighbor lands Jack in a string of comic adventures; a 2012 Newbery winner.
## 14 The offspring of evil must choose whether to follow in their parents footsteps; the junior novelization.
## 15 A young fantasy football guru hits the big-time.
## 16 A wrong-side-of-the-tracks hoops star joins a rival squad when he moves in with a foster family.
## 17 Trying to avoid a bully, two friends find much bigger trouble.
## 18 Washington leads his troops to victory in the Battle of Trenton.
## 19 A grandson is given his first guitar.
## 20 When his father is killed mysteriously, a boy hurtles toward superhero-dom.
## contributor
## 1 by Carmen Agra Deedy with Wilson Kimeli Naiyomah. Illustrated by Thomas Gonzalez
## 2 by Tony DiTerlizzi
## 3 by Adam Gidwitz and Jacob W. Grimm
## 4 by Herman Parish. Illustrated by Lynne Avril
## 5 by Herman Parish. Illustrated by Lynne Avril
## 6 by Jennifer A. Nielsen
## 7 by Nikki Grimes. Illustrated by Bryan Collier
## 8 by Jon Scieszka and Mac Barnett. Illustrated by Matthew Myers
## 9 by Steve Sheinkin
## 10 by Jeanne DuPrau
## 11 by Carl Hiaasen
## 12 by Darren Shan
## 13 by Jack Gantos
## 14 adapted by Rico Green
## 15 by Mike Lupica
## 16 by Mike Lupica
## 17 by Louis Sachar
## 18 by Jack E. Levin
## 19 by Keith Richards with Barnaby Harris and Bill Shapiro. Illustrated by Theodora Richards
## 20 by Mike Lupica
## author
## 1 Carmen Agra Deedy with Wilson Kimeli Naiyomah
## 2 Tony DiTerlizzi
## 3 Adam Gidwitz and Jacob W Grimm
## 4 Herman Parish
## 5 Herman Parish
## 6 Jennifer A Nielsen
## 7 Nikki Grimes
## 8 Jon Scieszka and Mac Barnett
## 9 Steve Sheinkin
## 10 Jeanne DuPrau
## 11 Carl Hiaasen
## 12 Darren Shan
## 13 Jack Gantos
## 14 adapted Rico Green
## 15 Mike Lupica
## 16 Mike Lupica
## 17 Louis Sachar
## 18 Jack E Levin
## 19 Keith Richards with Barna Harris and Bill Shapiro
## 20 Mike Lupica
## contributor_note price age_group
## 1 Illustrated by Thomas Gonzalez 17.95 Ages 6 to 10
## 2 17.99 Ages 10 to 14
## 3 0.00 Ages 10 and up
## 4 Illustrated by Lynne Avril 0.00 Ages 6 to 10
## 5 Illustrated by Lynne Avril 0.00 Ages 6 to 10
## 6 0.00 Ages 10 to 14
## 7 Illustrated by Bryan Collier 16.99 Ages 5 to 10
## 8 Illustrated by Matthew Myers 0.00 Ages 5 to 10
## 9 0.00 Ages 10 to 18
## 10 0.00 Ages 10 to 13
## 11 0.00 Ages 10 to 13
## 12 0.00 Ages 10 and up
## 13 15.99 Ages 10 to 14
## 14 0.00 Ages 10 to 14
## 15 0.00 Ages 10 to 14
## 16 0.00 Ages 10 to 14
## 17 0.00 Ages 10 to 14
## 18 0.00 Ages 10 to 14
## 19 Illustrated by Theodora Richards 0.00 Ages 4 to 10
## 20 17.99 Ages 10 and up
## publisher
## 1 Peachtree
## 2 Simon & Schuster
## 3 Penguin Group
## 4 Greenwillow/HarperCollins
## 5 Greenwillow/HarperCollins
## 6 Scholastic
## 7 Simon & Schuster
## 8 Simon & Schuster
## 9 Roaring Brook Press
## 10 Random House, hardcover and paper
## 11 Random House Publishing
## 12 Little, Brown, hardcover and paper
## 13 Farrar, Straus & Giroux
## 14 Disney Press
## 15 Penguin Group
## 16 Philomel
## 17 Delacorte
## 18 Threshold Editions
## 19 Megan Tingley/Little, Brown
## 20 Penguin
## isbns
## 1 1561454907, 9781561454907
## 2 1416983120, 9781416983125
## 3 0142419672, 0525423346, 9780142419670, 9780525423348
## 4 0062094963, 006209498X, 9780062094964, 9780062094988
## 5 0062094998, 9780062094995
## 6 0545284139, 0545284147, 0545284171, 0545633796, 0545284155, 0545284163, 9780545284134, 9780545284141, 9780545284172, 9780545633796, 9780545284158, 9780545284165
## 7 NULL
## 8 1442446730, 9781442446731
## 9 1596434872, 1596438614, 9781596434875, 9781596438613
## 10 NULL
## 11 0375868429, 0375968423, 0375868275, 0375898956, 9780375868429, 9780375968426, 9780375868276, 9780375898952
## 12 NULL
## 13 0374379939, 9780374379933
## 14 NULL
## 15 0399256075, 9780399256073
## 16 0399256067, 9780399256066
## 17 0385743785, 9780385743785
## 18 1476731934, 1476731942, 9781476731933, 9781476731940
## 19 031632065X, 9780316320658
## 20 0399252835, 9780399252839
## ranks_history
## 1 NULL
## 2 NULL
## 3 None, 9781101445280, 8, Childrens Middle Grade, Childrens Middle Grade, 2013-10-20, 2013-10-05, 4, NA, 0, 0
## 4 NULL
## 5 NULL
## 6 0545284171, 9780545284172, 7, Series Books, Childrens Series, 2014-03-16, 2014-03-01, 1, NA, 0, 0
## 7 NULL
## 8 1442446730, 9781442446731, 9, Picture Books, Childrens Picture Books, 2013-11-17, 2013-11-02, 1, NA, 0, 0
## 9 NULL
## 10 NULL
## 11 0375868275, 9780375868276, 15, Childrens Middle Grade, Childrens Middle Grade, 2013-09-15, 2013-08-31, 0, NA, 0, 0
## 12 NULL
## 13 NULL
## 14 1484726146, 1484726146, 1484726146, 1484726146, 1484726146, 9781484726143, 9781484726143, 9781484726143, 9781484726143, 9781484726143, 6, 4, 3, 3, 8, Childrens Middle Grade Hardcover, Childrens Middle Grade, Childrens Middle Grade, Childrens Middle Grade, Childrens Middle Grade, Childrens Middle Grade Hardcover, Childrens Middle Grade, Childrens Middle Grade, Childrens Middle Grade, Childrens Middle Grade, 2015-09-06, 2015-08-23, 2015-08-16, 2015-08-09, 2015-08-02, 2015-08-22, 2015-08-08, 2015-08-01, 2015-07-25, 2015-07-18, 1, 4, 3, 2, 1, NA, NA, NA, NA, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
## 15 0399256075, 0399256075, 0399256075, 0399256075, 9780399256073, 9780399256073, 9780399256073, 9780399256073, 15, 11, 14, 9, Childrens Middle Grade, Childrens Middle Grade, Childrens Middle Grade, Childrens Middle Grade, Childrens Middle Grade, Childrens Middle Grade, Childrens Middle Grade, Childrens Middle Grade, 2014-11-23, 2014-10-19, 2014-10-12, 2014-10-05, 2014-11-08, 2014-10-04, 2014-09-27, 2014-09-20, 0, 0, 0, 1, NA, NA, NA, NA, 0, 0, 0, 0, 0, 0, 0, 0
## 16 0399256067, 9780399256066, 10, Childrens Middle Grade Hardcover, Childrens Middle Grade Hardcover, 2015-11-22, 2015-11-07, 1, NA, 0, 0
## 17 0385743785, 0385743785, 0385743785, 9780385743785, 9780385743785, 9780385743785, 9, 10, 9, Childrens Middle Grade Hardcover, Childrens Middle Grade Hardcover, Childrens Middle Grade Hardcover, Childrens Middle Grade Hardcover, Childrens Middle Grade Hardcover, Childrens Middle Grade Hardcover, 2015-09-20, 2015-09-13, 2015-08-30, 2015-09-05, 2015-08-29, 2015-08-15, 3, 2, 1, NA, NA, NA, 0, 0, 0, 0, 0, 0
## 18 NULL
## 19 031632065X, 031632065X, 031632065X, 9780316320658, 9780316320658, 9780316320658, 2, 3, 3, Picture Books, Picture Books, Picture Books, Childrens Picture Books, Childrens Picture Books, Childrens Picture Books, 2014-10-12, 2014-10-05, 2014-09-28, 2014-09-27, 2014-09-20, 2014-09-13, 3, 2, 1, NA, NA, NA, 0, 0, 0, 0, 0, 0
## 20 NULL
## reviews
## 1 , , https://www.nytimes.com/2009/11/08/books/review/Kristof-t.html,
## 2 , , ,
## 3 , , ,
## 4 , , ,
## 5 , , ,
## 6 , , ,
## 7 , , https://www.nytimes.com/2008/10/12/books/review/Handy-t.html,
## 8 https://www.nytimes.com/2013/10/31/books/battle-bunny-by-jon-scieszka-and-mac-barnett.html, , ,
## 9 , , https://www.nytimes.com/2012/11/11/books/review/bomb-and-beyond-courage.html,
## 10 , , ,
## 11 , , ,
## 12 , , ,
## 13 , , ,
## 14 , , ,
## 15 , , ,
## 16 , , ,
## 17 , , https://www.nytimes.com/2015/11/08/books/review/fuzzy-mud-by-louis-sachar.html,
## 18 , , ,
## 19 , , https://www.nytimes.com/2014/11/09/books/review/jim-aylesworths-my-grandfathers-coat-and-more.html,
## 20 , , ,
# Verify Class, length and Names
class(maindata)
## [1] "list"
length(maindata)
## [1] 4
names(maindata)
## [1] "status" "copyright" "num_results" "results"
# Check number of elements in Lists
lapply(maindata, class)
## $status
## [1] "character"
##
## $copyright
## [1] "character"
##
## $num_results
## [1] "integer"
##
## $results
## [1] "data.frame"
lapply(maindata, length)
## $status
## [1] 1
##
## $copyright
## [1] 1
##
## $num_results
## [1] 1
##
## $results
## [1] 11
#Remove first three lists
results <- maindata[-c(1:3)]
names(results)
## [1] "results"
length(results)
## [1] 1
class(results)
## [1] "list"
results_df <- results[["results"]]
class(results_df)
## [1] "data.frame"
colnames(results_df)
## [1] "title" "description" "contributor"
## [4] "author" "contributor_note" "price"
## [7] "age_group" "publisher" "isbns"
## [10] "ranks_history" "reviews"
Books <- tbl_df(results_df)
Books <- select(Books, one_of(c("title", "description", "author", "price", "age_group", "publisher")))
Books <- rename(Books, Title = 'title', Description = 'description', Author = 'author', Price = 'price', Ages = 'age_group', Publisher = 'publisher')
Books
## # A tibble: 20 x 6
## Title Description Author Price Ages Publisher
## * <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 14 COWS FO~ Masai villagers in ~ Carmen Agra~ 18.0 Ages ~ Peachtree
## 2 A HERO FOR~ Something sinister ~ Tony DiTerl~ 18.0 Ages ~ Simon & Sch~
## 3 A TALE DAR~ Hansel and Gretel i~ Adam Gidwit~ 0 Ages ~ Penguin Gro~
## 4 AMELIA BED~ She wants a bike. Herman Pari~ 0 Ages ~ Greenwillow~
## 5 AMELIA BED~ Shes getting a pup~ Herman Pari~ 0 Ages ~ Greenwillow~
## 6 ASCENDANCE~ The kingdom of Cart~ Jennifer A ~ 0 Ages ~ Scholastic
## 7 BARACK OBA~ Yes, he can; a chil~ Nikki Grimes 17.0 Ages ~ Simon & Sch~
## 8 BATTLE BUN~ With pencil edits, ~ Jon Scieszk~ 0 Ages ~ Simon & Sch~
## 9 BOMB The cross-continent~ Steve Shein~ 0 Ages ~ Roaring Bro~
## 10 BOOKS OF E~ In a postapocalypti~ Jeanne DuPr~ 0 Ages ~ Random Hous~
## 11 CHOMP Life gets wilder fo~ Carl Hiaasen 0 Ages ~ Random Hous~
## 12 CIRQUE DU ~ A vampires assista~ Darren Shan 0 Ages ~ Little, Bro~
## 13 DEAD END I~ Typing obituaries f~ Jack Gantos 16.0 Ages ~ Farrar, Str~
## 14 DESCENDANTS The offspring of ev~ adapted Ri~ 0 Ages ~ Disney Press
## 15 FANTASY LE~ A young fantasy foo~ Mike Lupica 0 Ages ~ Penguin Gro~
## 16 FAST BREAK A wrong-side-of-the~ Mike Lupica 0 Ages ~ Philomel
## 17 FUZZY MUD Trying to avoid a b~ Louis Sachar 0 Ages ~ Delacorte
## 18 GEORGE WAS~ Washington leads hi~ Jack E Levin 0 Ages ~ Threshold E~
## 19 GUS & ME A grandson is given~ Keith Richa~ 0 Ages ~ Megan Tingl~
## 20 HERO When his father is ~ Mike Lupica 18.0 Ages ~ Penguin
datatable(Books)