This R markdown file represents partial completion of IS 607, a course in the MS in Data Analytics degree at The City University of New York.
We’ve already obtained an API key from the New York Times at their website, http://developer.nytimes.com/. We’ll get a list of the best-selling book categories:
library(jsonlite)
##
## Attaching package: 'jsonlite'
##
## The following object is masked from 'package:utils':
##
## View
best_seller_types<-fromJSON("http://api.nytimes.com/svc/books/v3/lists/names.json?api-key=7b3c008dec513c583fdaddf07142b062%3A8%3A72761959")
best_seller_types<-best_seller_types[[4]]
head(best_seller_types)
## list_name display_name
## 1 Combined Print and E-Book Fiction Combined Print & E-Book Fiction
## 2 Combined Print and E-Book Nonfiction Combined Print & E-Book Nonfiction
## 3 Hardcover Fiction Hardcover Fiction
## 4 Hardcover Nonfiction Hardcover Nonfiction
## 5 Trade Fiction Paperback Paperback Trade Fiction
## 6 Mass Market Paperback Paperback Mass-Market Fiction
## list_name_encoded oldest_published_date
## 1 combined-print-and-e-book-fiction 2011-02-13
## 2 combined-print-and-e-book-nonfiction 2011-02-13
## 3 hardcover-fiction 2008-06-08
## 4 hardcover-nonfiction 2008-06-08
## 5 trade-fiction-paperback 2008-06-08
## 6 mass-market-paperback 2008-06-08
## newest_published_date updated
## 1 2015-11-01 WEEKLY
## 2 2015-11-01 WEEKLY
## 3 2015-11-01 WEEKLY
## 4 2015-11-01 WEEKLY
## 5 2015-11-01 WEEKLY
## 6 2015-11-01 WEEKLY
Now I’d like to do a comparison of how the senators from my home state of Pennsylvania vote.
First, I’ll get their member numbers, using a different API key designated for the Congress API:
my_senators<-fromJSON("http://api.nytimes.com/svc/politics/v3/us/legislative/congress/members/senate/PA/current.json?api-key=0090657b5765236fe15b476ab8dab011%3A4%3A72761959")
my_senators<-my_senators[[3]]
head(my_senators)
## id name role gender party
## 1 C001070 Bob Casey Senator, 1st Class M D
## 2 T000461 Patrick J. Toomey Senator, 3rd Class M R
## times_topics_url
## 1 http://topics.nytimes.com/top/reference/timestopics/people/c/robert_p_jr_casey/index.html
## 2
## twitter_id youtube_id seniority next_election
## 1 SenBobCasey SenatorBobCasey 9 2018
## 2 SenToomey sentoomey 5 2016
## api_url
## 1 http://api.nytimes.com/svc/politics/v3/us/legislative/congress/members/C001070.json
## 2 http://api.nytimes.com/svc/politics/v3/us/legislative/congress/members/T000461.json
first_id<-my_senators[1,"id"]
second_id<-my_senators[2,"id"]
Now I’ll see how they compare:
my_url<-paste("http://api.nytimes.com/svc/politics/v3/us/legislative/congress/members/", first_id, "/votes/", second_id, "/113/senate.json?api-key=0090657b5765236fe15b476ab8dab011%3A4%3A72761959", sep="")
my_url
## [1] "http://api.nytimes.com/svc/politics/v3/us/legislative/congress/members/C001070/votes/T000461/113/senate.json?api-key=0090657b5765236fe15b476ab8dab011%3A4%3A72761959"
voting_comparison<-fromJSON(my_url)
voting_comparison<-voting_comparison[[3]]
head(voting_comparison)
## first_member_id
## 1 C001070
## first_member_api_uri
## 1 http://api.nytimes.com/svc/politics/v3/us/legislative/congress/members/C001070.json
## second_member_id
## 1 T000461
## second_member_api_uri
## 1 http://api.nytimes.com/svc/politics/v3/us/legislative/congress/members/T000461.json
## congress chamber common_votes disagree_votes agree_percent
## 1 113 Senate 615 403 34.47
## disagree_percent
## 1 65.53