The 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 data frame.For this assignment, I chose the Movie Review API from the NYT’s website. One drawback of this API is that paging is required. As a result, only 20 movies are returned at a time.
library(jsonlite)
library(dplyr)
#API key from the NYT wesbsite for Movie Reviews
key <- "&api-key=e13903a0a60761ade30cbf94df0f25cc:7:73345107"
#Request to access movie review from API.
url <- "http://api.nytimes.com/svc/movies/v2/reviews/dvd-picks.json?order=by-title"
#Query API data from NYTimes website. The JSON function automatically
query <- fromJSON(paste0(url, key))
reviews <- query$results
colnames(reviews)
## [1] "nyt_movie_id" "display_title" "sort_name"
## [4] "mpaa_rating" "critics_pick" "thousand_best"
## [7] "byline" "headline" "capsule_review"
## [10] "summary_short" "publication_date" "opening_date"
## [13] "dvd_release_date" "date_updated" "seo_name"
## [16] "link" "related_urls" "multimedia"
Keeping Columns of Interest Only
Using the select function from dplyr to keep only column of interest from the data frame.
#selecting desired columns
reviews_new <- select(reviews,nyt_movie_id, display_title, mpaa_rating, critics_pick, headline, summary_short,publication_date)
#column names
colnames(reviews_new)
## [1] "nyt_movie_id" "display_title" "mpaa_rating"
## [4] "critics_pick" "headline" "summary_short"
## [7] "publication_date"
#taking a peek
head(reviews_new)
## nyt_movie_id display_title mpaa_rating
## 1 463724 ! Women Art Revolution: A Secret History <NA>
## 2 475603 '71 R
## 3 339982 'Tis Autumn: The Search for Jackie Paris <NA>
## 4 316637 ...So Goes the Nation <NA>
## 5 471990 1 <NA>
## 6 5 10 R
## critics_pick headline
## 1 1 Enhancing the Image of Feminist Art
## 2 1 Review: In <U+0091> <U+0092>71,<U+0092> Young, Green and Behind Enemy Lines
## 3 1 A Very Late Comeback
## 4 1 A Look Back at 2004, When Ohio Was Up for Grabs
## 5 1 Revelers, Pause to Meet Yourselves
## 6 1
## summary_short
## 1 A documentary makes the case that the feminist art movement has been more influential than is generally acknowledged.
## 2 In Belfast, 1971 was an eventful year of demands and bloodshed, and it was not always clear who was the enemy.
## 3 Part tribute, part musical mystery, <U+0093> <U+0092>Tis Autumn: The Search for Jackie Paris<U+0094> shines an overdue spotlight on a great who got away.
## 4 A <U+0091>what went wrong?<U+0092> documentary that succinctly and ruthlessly dissects the battle for Ohio in the months leading up to the 2004 presidential election.
## 5 A bawdy college kegger has a possible extraterrestrial mystery in “+1.<U+0094>
## 6
## publication_date
## 1 2011-06-01
## 2 2015-02-27
## 3 2007-12-07
## 4 2006-10-04
## 5 2013-09-20
## 6 1979-10-05
#data structure to confirm it is a data frame.
str(reviews_new)
## 'data.frame': 20 obs. of 7 variables:
## $ nyt_movie_id : int 463724 475603 339982 316637 471990 5 388301 451556 451146 335078 ...
## $ display_title : chr "! Women Art Revolution: A Secret History" "'71" "'Tis Autumn: The Search for Jackie Paris" "...So Goes the Nation" ...
## $ mpaa_rating : chr NA "R" NA NA ...
## $ critics_pick : int 1 1 1 1 1 1 1 1 1 1 ...
## $ headline : chr "Enhancing the Image of Feminist Art" "Review: In <U+0091> <U+0092>71,<U+0092> Young, Green and Behind Enemy Lines""| __truncated__ "A Very Late Comeback" "A Look Back at 2004, When Ohio Was Up for Grabs" ...
## $ summary_short : chr "A documentary makes the case that the feminist art movement has been more influential than is generally acknowledged." "In Belfast, 1971 was an eventful year of demands and bloodshed, and it was not always clear who was the enemy." "Part tribute, part musical mystery, <U+0093> <U+0092>Tis Autumn: The Search for Jackie Paris<U+0094> shines an overdue spotlight on a great who got "| __truncated__ "A <U+0091>what went wrong?<U+0092> documentary that succinctly and ruthlessly dissects the battle for Ohio in the months leading up to the 20"| __truncated__ ...
## $ publication_date: chr "2011-06-01" "2015-02-27" "2007-12-07" "2006-10-04" ...