This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(RJSONIO)
## Warning: package 'RJSONIO' was built under R version 3.1.3
library (RCurl)
## Warning: package 'RCurl' was built under R version 3.1.3
## Loading required package: bitops

Base URL: http://api.nytimes.com/svc/search/v2/articlesearch

1. set API key for The Article Search API

key <- "fd34e262ad7a11aee6eeed7610de5917:19:71868662"

2. Query for the word " CUNY" in all articles

query <- "CUNY" # Search string
records <- 500 # number of records we want 
return_rec<- records/10  
pageRange <- 1:return_rec

3. Find the occurances of the word ‘CUNY’ in the first 50 pages

We are searching for both the headline and date in which the word ‘CUNY’ had appeared

for (i in pageRange) {
  # concatenate URL for each page
  uri <- paste0("http://api.nytimes.com/svc/search/v2/articlesearch.json?q=", query, "&page=", i, "&fl=headline,pub_date", "&api-key=", key)
  d <- getURL(uri)
  res <- fromJSON(d,simplify = FALSE)
}
#str(res)
#res$response$docs$headline$main
#str(res_df)
#res$status
#res$copyright
#res$response$meta$hits
#length(res)

4. Extract onlythe headline and date from the result set

# initializing the 'CUNY_H_D' structure. it is CUNY headline and date structure 

CUNY_H_D<- c()
CUNY_H_D2<- c()
length(CUNY_H_D)= 0
length(CUNY_H_D2)= 0

for ( i in 1:length(CUNY_H_D)) {CUNY_H_D[i]<- 0}
length(CUNY_H_D)= 0

5. Extract only the main headline and date from the JSON returned results

i=0
for ( i in 1:10) {
  CUNY_H_D<- c(CUNY_H_D, res$response$docs[[i]]$headline$main, res$response$docs[[i]]$pub_date,collapse = NULL)
  
  # using paste function as well to extract only the main headline and date from the JSON returned results
  CUNY_H_D2<- paste(c(CUNY_H_D2, res$response$docs[[i]]$headline$main, res$response$docs[[i]]$pub_date),sep= ",")
}

6. Display the CUNY main headlines and dates

data.frame(CUNY_H_D)
##                                                    CUNY_H_D
## 1          CUNY Chief Gives Tenure To Professor In Brooklyn
## 2                                      2003-02-25T00:00:00Z
## 3        CUNY Reports a 5% Jump In Enrollment for Fall Term
## 4                                      2002-09-13T00:00:00Z
## 5   CUNY, Short on Faculty, Is to End Free Term for Seniors
## 6                                      2002-02-06T00:00:00Z
## 7   CUNY Raises Tuition Rates For Foreigners Here Illegally
## 8                                      2001-11-03T00:00:00Z
## 9        Dr. Geoffrey Marshall, 62, Who Was Provost at CUNY
## 10                                     2000-11-25T00:00:00Z
## 11            Critic of CUNY, Badillo, Chosen To Head Board
## 12                                     1999-05-31T00:00:00Z
## 13  Yes, Teacher: Giuliani Scolds CUNY on Attendance Policy
## 14                                     1998-04-25T00:00:00Z
## 15    Educator Joseph Murphy, 64, Former Chancellor of CUNY
## 16                                     1998-01-18T00:00:00Z
## 17                  On York Campus, Ire Over CUNY Land Deal
## 18                                     1995-11-12T00:00:00Z
## 19 Colleges to Offer Their Ideas In Battle to Overhaul CUNY
## 20                                     1993-11-01T00:00:00Z
CUNY_H_D
##                                                            
##         "CUNY Chief Gives Tenure To Professor In Brooklyn" 
##                                                            
##                                     "2003-02-25T00:00:00Z" 
##                                                            
##       "CUNY Reports a 5% Jump In Enrollment for Fall Term" 
##                                                            
##                                     "2002-09-13T00:00:00Z" 
##                                                            
##  "CUNY, Short on Faculty, Is to End Free Term for Seniors" 
##                                                            
##                                     "2002-02-06T00:00:00Z" 
##                                                            
##  "CUNY Raises Tuition Rates For Foreigners Here Illegally" 
##                                                            
##                                     "2001-11-03T00:00:00Z" 
##                                                            
##       "Dr. Geoffrey Marshall, 62, Who Was Provost at CUNY" 
##                                                            
##                                     "2000-11-25T00:00:00Z" 
##                                                            
##            "Critic of CUNY, Badillo, Chosen To Head Board" 
##                                                            
##                                     "1999-05-31T00:00:00Z" 
##                                                            
##  "Yes, Teacher: Giuliani Scolds CUNY on Attendance Policy" 
##                                                            
##                                     "1998-04-25T00:00:00Z" 
##                                                            
##    "Educator Joseph Murphy, 64, Former Chancellor of CUNY" 
##                                                            
##                                     "1998-01-18T00:00:00Z" 
##                                                            
##                  "On York Campus, Ire Over CUNY Land Deal" 
##                                                            
##                                     "1995-11-12T00:00:00Z" 
##                                                            
## "Colleges to Offer Their Ideas In Battle to Overhaul CUNY" 
##                                                            
##                                     "1993-11-01T00:00:00Z"
CUNY_H_D2
##  [1] "CUNY Chief Gives Tenure To Professor In Brooklyn"        
##  [2] "2003-02-25T00:00:00Z"                                    
##  [3] "CUNY Reports a 5% Jump In Enrollment for Fall Term"      
##  [4] "2002-09-13T00:00:00Z"                                    
##  [5] "CUNY, Short on Faculty, Is to End Free Term for Seniors" 
##  [6] "2002-02-06T00:00:00Z"                                    
##  [7] "CUNY Raises Tuition Rates For Foreigners Here Illegally" 
##  [8] "2001-11-03T00:00:00Z"                                    
##  [9] "Dr. Geoffrey Marshall, 62, Who Was Provost at CUNY"      
## [10] "2000-11-25T00:00:00Z"                                    
## [11] "Critic of CUNY, Badillo, Chosen To Head Board"           
## [12] "1999-05-31T00:00:00Z"                                    
## [13] "Yes, Teacher: Giuliani Scolds CUNY on Attendance Policy" 
## [14] "1998-04-25T00:00:00Z"                                    
## [15] "Educator Joseph Murphy, 64, Former Chancellor of CUNY"   
## [16] "1998-01-18T00:00:00Z"                                    
## [17] "On York Campus, Ire Over CUNY Land Deal"                 
## [18] "1995-11-12T00:00:00Z"                                    
## [19] "Colleges to Offer Their Ideas In Battle to Overhaul CUNY"
## [20] "1993-11-01T00:00:00Z"