Set up configuration for JSON
#Set API key for Top Stories
API_KEY = "?api-key=6b601306f2e34f3ab7f572ca3edf8f35"
URL = "https://api.nytimes.com/svc/topstories/v2/home.json"
API_URL = paste(URL,API_KEY,sep="")
API_URL
## [1] "https://api.nytimes.com/svc/topstories/v2/home.json?api-key=6b601306f2e34f3ab7f572ca3edf8f35"
#Call necessary libraries
library(jsonlite)
## Warning: package 'jsonlite' was built under R version 3.4.1
library(stringr)
## Warning: package 'stringr' was built under R version 3.4.1
library(knitr)
## Warning: package 'knitr' was built under R version 3.4.4
library(tidyr)
## Warning: package 'tidyr' was built under R version 3.4.1
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.4.3
Retrieve data from API and manipulate output
#Read JSON using API key
topstories <- fromJSON(API_URL, flatten = TRUE)
#Convert JSON into data.frame
topstories <- data.frame(topstories)
colnames(topstories)
## [1] "status" "copyright"
## [3] "section" "last_updated"
## [5] "num_results" "results.section"
## [7] "results.subsection" "results.title"
## [9] "results.abstract" "results.url"
## [11] "results.byline" "results.item_type"
## [13] "results.updated_date" "results.created_date"
## [15] "results.published_date" "results.material_type_facet"
## [17] "results.kicker" "results.des_facet"
## [19] "results.org_facet" "results.per_facet"
## [21] "results.geo_facet" "results.multimedia"
## [23] "results.short_url"
#Choose only necessary columns
topstories_filtered <- topstories[c(6,8,10,12,15,11)]
head(topstories_filtered)
## results.section
## 1 Briefing
## 2 Climate
## 3 World
## 4 World
## 5 World
## 6 U.S.
## results.title
## 1 Russia, Malala, North Korea: Your Friday Briefing
## 2 E.P.A. Prepares to Roll Back Rules Requiring Cars to Be Cleaner and More Efficient
## 3 Russia to Expel 60 U.S. Diplomats; Tensions Are Worst in Decades
## 4 Britain Signals Harder Look at Wealthy Russians and Russian Wealth
## 5 Yulia Skripal, Poisoned Daughter of Ex-Spy, Is Out of Critical Condition
## 6 Trump Attacks Amazon, Saying It Does Not Pay Enough Taxes
## results.url
## 1 https://www.nytimes.com/2018/03/29/briefing/russia-malala-north-korea.html
## 2 https://www.nytimes.com/2018/03/29/climate/epa-cafe-auto-pollution-rollback.html
## 3 https://www.nytimes.com/2018/03/29/world/europe/russia-expels-diplomats.html
## 4 https://www.nytimes.com/2018/03/29/world/europe/uk-britain-russia-russian-wealth-putin.html
## 5 https://www.nytimes.com/2018/03/29/world/europe/britain-yulia-skripal-hospital.html
## 6 https://www.nytimes.com/2018/03/29/us/politics/trump-amazon-taxes.html
## results.item_type results.published_date
## 1 Article 2018-03-29T15:14:50-04:00
## 2 Article 2018-03-29T16:22:03-04:00
## 3 Article 2018-03-29T13:18:44-04:00
## 4 Article 2018-03-29T12:16:37-04:00
## 5 Article 2018-03-29T11:07:16-04:00
## 6 Article 2018-03-29T08:34:22-04:00
## results.byline
## 1 By CHARLES McDERMID and INYOUNG KANG
## 2 By CORAL DAVENPORT and HIROKO TABUCHI
## 3 By RICHARD PÉREZ-PEÑA
## 4 By RICHARD PÉREZ-PEÑA
## 5 By MICHAEL WOLGELENTER and ILIANA MAGRA
## 6 By EILEEN SULLIVAN and NICK WINGFIELD
#Change the column names
colnames(topstories_filtered)
## [1] "results.section" "results.title"
## [3] "results.url" "results.item_type"
## [5] "results.published_date" "results.byline"
colnames(topstories_filtered) <- c("section", "title", "url", "item_type", "published_date", "author")
head(topstories_filtered)
## section
## 1 Briefing
## 2 Climate
## 3 World
## 4 World
## 5 World
## 6 U.S.
## title
## 1 Russia, Malala, North Korea: Your Friday Briefing
## 2 E.P.A. Prepares to Roll Back Rules Requiring Cars to Be Cleaner and More Efficient
## 3 Russia to Expel 60 U.S. Diplomats; Tensions Are Worst in Decades
## 4 Britain Signals Harder Look at Wealthy Russians and Russian Wealth
## 5 Yulia Skripal, Poisoned Daughter of Ex-Spy, Is Out of Critical Condition
## 6 Trump Attacks Amazon, Saying It Does Not Pay Enough Taxes
## url
## 1 https://www.nytimes.com/2018/03/29/briefing/russia-malala-north-korea.html
## 2 https://www.nytimes.com/2018/03/29/climate/epa-cafe-auto-pollution-rollback.html
## 3 https://www.nytimes.com/2018/03/29/world/europe/russia-expels-diplomats.html
## 4 https://www.nytimes.com/2018/03/29/world/europe/uk-britain-russia-russian-wealth-putin.html
## 5 https://www.nytimes.com/2018/03/29/world/europe/britain-yulia-skripal-hospital.html
## 6 https://www.nytimes.com/2018/03/29/us/politics/trump-amazon-taxes.html
## item_type published_date
## 1 Article 2018-03-29T15:14:50-04:00
## 2 Article 2018-03-29T16:22:03-04:00
## 3 Article 2018-03-29T13:18:44-04:00
## 4 Article 2018-03-29T12:16:37-04:00
## 5 Article 2018-03-29T11:07:16-04:00
## 6 Article 2018-03-29T08:34:22-04:00
## author
## 1 By CHARLES McDERMID and INYOUNG KANG
## 2 By CORAL DAVENPORT and HIROKO TABUCHI
## 3 By RICHARD PÉREZ-PEÑA
## 4 By RICHARD PÉREZ-PEÑA
## 5 By MICHAEL WOLGELENTER and ILIANA MAGRA
## 6 By EILEEN SULLIVAN and NICK WINGFIELD
#You can subset data frame by column. This time, I filtered data frame by section == "World"
subset(topstories_filtered, section == "World")
## section
## 3 World
## 4 World
## 5 World
## 20 World
## 21 World
## 23 World
## title
## 3 Russia to Expel 60 U.S. Diplomats; Tensions Are Worst in Decades
## 4 Britain Signals Harder Look at Wealthy Russians and Russian Wealth
## 5 Yulia Skripal, Poisoned Daughter of Ex-Spy, Is Out of Critical Condition
## 20 Deal With U.S. Military Sets Off Protests in Ghana
## 21 India Spurns the Dalai Lama<U+0092>s Celebration, Worried About China
## 23 After Dozens Die in a Jail Fire, Venezuela Tear-Gasses Their Relatives
## url
## 3 https://www.nytimes.com/2018/03/29/world/europe/russia-expels-diplomats.html
## 4 https://www.nytimes.com/2018/03/29/world/europe/uk-britain-russia-russian-wealth-putin.html
## 5 https://www.nytimes.com/2018/03/29/world/europe/britain-yulia-skripal-hospital.html
## 20 https://www.nytimes.com/2018/03/28/world/africa/ghana-us-military-deal-protests.html
## 21 https://www.nytimes.com/2018/03/29/world/asia/dalai-lama-india-china-.html
## 23 https://www.nytimes.com/2018/03/29/world/americas/venezuela-jail-fire.html
## item_type published_date
## 3 Article 2018-03-29T13:18:44-04:00
## 4 Article 2018-03-29T12:16:37-04:00
## 5 Article 2018-03-29T11:07:16-04:00
## 20 Article 2018-03-28T15:20:56-04:00
## 21 Article 2018-03-29T05:02:53-04:00
## 23 Article 2018-03-29T12:32:01-04:00
## author
## 3 By RICHARD PÉREZ-PEÑA
## 4 By RICHARD PÉREZ-PEÑA
## 5 By MICHAEL WOLGELENTER and ILIANA MAGRA
## 20 By DIONNE SEARCEY and ERIC SCHMITT
## 21 By MARIA ABI-HABIB
## 23 By ANA VANESSA HERRERO
#You can also create tables for better presentation
topstories_filtered_table <- kable(topstories_filtered)
topstories_filtered_table
| section | title | url | item_type | published_date | author |
|---|---|---|---|---|---|
| Briefing | Russia, Malala, North Korea: Your Friday Briefing | https://www.nytimes.com/2018/03/29/briefing/russia-malala-north-korea.html | Article | 2018-03-29T15:14:50-04:00 | By CHARLES McDERMID and INYOUNG KANG |
| Climate | E.P.A. Prepares to Roll Back Rules Requiring Cars to Be Cleaner and More Efficient | https://www.nytimes.com/2018/03/29/climate/epa-cafe-auto-pollution-rollback.html | Article | 2018-03-29T16:22:03-04:00 | By CORAL DAVENPORT and HIROKO TABUCHI |
| World | Russia to Expel 60 U.S. Diplomats; Tensions Are Worst in Decades | https://www.nytimes.com/2018/03/29/world/europe/russia-expels-diplomats.html | Article | 2018-03-29T13:18:44-04:00 | By RICHARD PÉREZ-PEÑA |
| World | Britain Signals Harder Look at Wealthy Russians and Russian Wealth | https://www.nytimes.com/2018/03/29/world/europe/uk-britain-russia-russian-wealth-putin.html | Article | 2018-03-29T12:16:37-04:00 | By RICHARD PÉREZ-PEÑA |
| World | Yulia Skripal, Poisoned Daughter of Ex-Spy, Is Out of Critical Condition | https://www.nytimes.com/2018/03/29/world/europe/britain-yulia-skripal-hospital.html | Article | 2018-03-29T11:07:16-04:00 | By MICHAEL WOLGELENTER and ILIANA MAGRA |
| U.S. | Trump Attacks Amazon, Saying It Does Not Pay Enough Taxes | https://www.nytimes.com/2018/03/29/us/politics/trump-amazon-taxes.html | Article | 2018-03-29T08:34:22-04:00 | By EILEEN SULLIVAN and NICK WINGFIELD |
| Technology | Can Social Media Be Saved? | https://www.nytimes.com/2018/03/28/technology/social-media-privacy.html | Article | 2018-03-28T14:03:42-04:00 | By KEVIN ROOSE |
| Business Day | Facebook Falls From Grace, and Investors’ Stock Holdings Tumble Too | https://www.nytimes.com/2018/03/29/business/facebook-stock.html | Article | 2018-03-29T11:46:12-04:00 | By JAMES B. STEWART |
| U.S. | Trump’s Lawyer Raised Prospect of Pardons for Flynn and Manafort | https://www.nytimes.com/2018/03/28/us/politics/trump-pardon-michael-flynn-paul-manafort-john-dowd.html | Article | 2018-03-28T13:19:31-04:00 | By MICHAEL S. SCHMIDT, JO BECKER, MARK MAZZETTI, MAGGIE HABERMAN and ADAM GOLDMAN |
| U.S. | Trump Aide Spoke During Campaign to Associate Tied to Russian Intelligence | https://www.nytimes.com/2018/03/28/us/politics/rick-gates-trump-campaign-russian-intelligence.html | Article | 2018-03-28T11:35:46-04:00 | By MARK MAZZETTI |
| U.S. | Justice Dept. Will Be Investigated Over Surveillance of Trump Campaign Official | https://www.nytimes.com/2018/03/28/us/politics/justice-department-carter-page-surveillance.html | Article | 2018-03-28T19:51:43-04:00 | By NICHOLAS FANDOS and KATIE BENNER |
| Business Day | To Avoid More Racist Hoodies, Retailers Seek Diversity | https://www.nytimes.com/2018/03/29/business/fast-fashion-diversity.html | Article | 2018-03-29T11:18:00-04:00 | By TIFFANY HSU |
| Opinion | Passover in a Land of Jewish Ghosts | https://www.nytimes.com/2018/03/29/opinion/passover-in-a-land-of-jewish-ghosts.html | Article | 2018-03-29T12:05:01-04:00 | By DAVID WOLPE |
| Opinion | Gen X Activism: An Oral History | https://www.nytimes.com/2018/03/29/opinion/sunday/gen-x-activism.html | Article | 2018-03-29T15:28:22-04:00 | By DEVORAH BLACHOR |
| Opinion | The 2016 Exit Polls Led Us to Misinterpret the 2016 Election | https://www.nytimes.com/2018/03/29/opinion/2016-exit-polls-election.html | Article | 2018-03-29T05:45:05-04:00 | By THOMAS B. EDSALL |
| Opinion | How Much Stadium Netting is Too Much? | https://www.nytimes.com/2018/03/29/opinion/baseball-stadium-netting.html | Article | 2018-03-29T15:56:48-04:00 | By PETER FUNT |
| Opinion | David J. Shulkin: Privatizing the V.A. Will Hurt Veterans | https://www.nytimes.com/2018/03/28/opinion/shulkin-veterans-affairs-privatization.html | Article | 2018-03-28T23:47:55-04:00 | By DAVID J. SHULKIN |
| U.S. | Adnan Syed of ‘Serial’ Is Granted a New Trial | https://www.nytimes.com/2018/03/29/us/adnan-syed-serial.html | Article | 2018-03-29T13:55:30-04:00 | By LIAM STACK |
| Business Day | Laura Ingraham, Facing Boycott, Apologizes for Taunting Parkland Survivor David Hogg | https://www.nytimes.com/2018/03/29/business/media/laura-ingraham-david-hogg.html | Article | 2018-03-29T14:52:25-04:00 | By DANIEL VICTOR |
| World | Deal With U.S. Military Sets Off Protests in Ghana | https://www.nytimes.com/2018/03/28/world/africa/ghana-us-military-deal-protests.html | Article | 2018-03-28T15:20:56-04:00 | By DIONNE SEARCEY and ERIC SCHMITT |
| World | India Spurns the Dalai Lama’s Celebration, Worried About China | https://www.nytimes.com/2018/03/29/world/asia/dalai-lama-india-china-.html | Article | 2018-03-29T05:02:53-04:00 | By MARIA ABI-HABIB |
| Reader Center | ‘He Is Not a Victim’: Our Austin Bomber Coverage Explained | https://www.nytimes.com/2018/03/28/reader-center/austin-bomber-media-coverage.html | Article | 2018-03-28T17:28:26-04:00 | By THE NEW YORK TIMES |
| World | After Dozens Die in a Jail Fire, Venezuela Tear-Gasses Their Relatives | https://www.nytimes.com/2018/03/29/world/americas/venezuela-jail-fire.html | Article | 2018-03-29T12:32:01-04:00 | By ANA VANESSA HERRERO |
| U.S. | In Arizona Race, Conservatives View Stormy Daniels as a ‘Little Hard to Believe’ | https://www.nytimes.com/2018/03/28/us/arizona-race-trump-stormy-daniels-lesko-tipirneni.html | Article | 2018-03-28T20:17:57-04:00 | By JULIE TURKEWITZ |
| U.S. | Hard Choice for Cities Under Cyberattack: Whether to Pay Ransom | https://www.nytimes.com/2018/03/29/us/atlanta-cyberattack-ransom.html | Article | 2018-03-29T14:50:54-04:00 | By ALAN BLINDER and NICOLE PERLROTH |
| N.Y. / Region | Cuomo, in Writing, Reinterprets Fund-Raising Ban on Appointees | https://www.nytimes.com/2018/03/29/nyregion/cuomo-appointee-donations-campaign-finance.html | Article | 2018-03-29T15:08:38-04:00 | By SHANE GOLDMACHER and BRIAN M. ROSENTHAL |
| Style | The Last Great Clothing Store | https://www.nytimes.com/2018/03/29/style/the-last-great-clothing-store-boyds.html | Article | 2018-03-29T00:00:01-04:00 | By STEVEN KURUTZ |
| Arts | I Spy at New York’s Museum of Deception | https://www.nytimes.com/2018/03/29/arts/design/spyscape-espionage-museum.html | Article | 2018-03-29T10:00:10-04:00 | By WILLIAM L. HAMILTON |
| Books | Can Islamic and European Civilizations Coexist? | https://www.nytimes.com/2018/03/28/books/review/journey-into-europe-akbar-ahmed.html | Article | 2018-03-28T16:00:08-04:00 | By ATOSSA ARAXIA ABRAHAMIAN |
| Smarter Living | 5 Cheap(ish) Things to Help You Become a Better Baker | https://www.nytimes.com/2018/03/29/smarter-living/5-cheap-ish-things-to-help-you-become-a-better-baker.html | Article | 2018-03-29T02:45:01-04:00 | By MICHAEL GOLD |
| Well | Finding Meaning and Happiness in Old Age | https://www.nytimes.com/2018/03/19/well/finding-meaning-and-happiness-in-old-age.html | Article | 2018-03-19T06:00:01-04:00 | By JANE E. BRODY |
| Well | You May Be Using Your Child’s Car Seat Incorrectly | https://www.nytimes.com/2018/03/20/well/family/you-may-be-using-your-childs-car-seat-incorrectly.html | Article | 2018-03-20T06:00:01-04:00 | By RACHEL RABKIN PEACHMAN |
| Real Estate | How to Turn Your Renovation Into a Vacation | https://www.nytimes.com/2018/03/20/realestate/paying-for-renovation-with-rewards-credit-cards.html | Article | 2018-03-20T05:00:12-04:00 | By MICHELLE HIGGINS |
| Magazine | The Comfort in Stockpiling Dried Beans | https://www.nytimes.com/2018/03/20/magazine/dried-beans-garlic-toast-broth-recipe.html | Article | 2018-03-20T05:00:01-04:00 | By TEJAL RAO |
| Arts | Snooki, JWoww and the Crew Return to MTV in ‘Jersey Shore Family Vacation’ | https://www.nytimes.com/2018/03/29/arts/television/mtv-jersey-shore-family-vacation.html | Article | 2018-03-29T05:00:14-04:00 | By DAVE ITZKOFF |
| Arts | ‘The Americans’: Will Holly Taylor Inherit the Family Business? | https://www.nytimes.com/2018/03/28/arts/television/the-americans-holly-taylor.html | Article | 2018-03-28T18:04:54-04:00 | By JONATHAN SMALL |
| Style | What Can an Online Dating Coach Do for You? | https://www.nytimes.com/2018/03/28/style/should-i-hire-a-dating-coach.html | Article | 2018-03-28T12:05:12-04:00 | By JENNIFER MILLER |
| T Magazine | How a Rising Rock Band Shops for Vintage | https://www.nytimes.com/2018/03/29/t-magazine/sunflower-bean-julia-cumming-vintage-shopping.html | Article | 2018-03-29T12:22:56-04:00 | By ALICE HINES |
| Theater | In Munich and Stuttgart, Directors Give the Classics a Twist | https://www.nytimes.com/2018/03/29/theater/munich-stuttgart-germany-theater.html | Article | 2018-03-29T00:18:24-04:00 | By A.J. GOLDMANN |
| Science | Chile and Its Scientists Protest Research on Tiny Mummy | https://www.nytimes.com/2018/03/28/science/atacama-mummy-chile.html | Article | 2018-03-28T19:43:49-04:00 | By CARL ZIMMER |
| Science | Earliest Known Human Footprints in North America Found on Canadian Island | https://www.nytimes.com/2018/03/28/science/footprints-oldest-north-america.html | Article | 2018-03-28T14:00:05-04:00 | By NICHOLAS ST. FLEUR |
| Science | How to Spot a Nuclear Bomb Program? Look for Ghostly Particles | https://www.nytimes.com/2018/03/27/science/nuclear-bombs-antineutrinos.html | Article | 2018-03-27T17:54:44-04:00 | By KENNETH CHANG |
| Well | Those 2-Minute Walk Breaks? They Add Up | https://www.nytimes.com/2018/03/28/well/move/walking-exercise-minutes-death-longevity.html | Article | 2018-03-28T00:32:04-04:00 | By GRETCHEN REYNOLDS |