The purpose of this assignment is to work with a public New York Times API using R. I will retrieve data through an API request, process the JSON response, and organize the information into a tidy data frame for analysis.
For this assignment, I selected the New York Times Most Popular API, which provides information about articles that receive the most views, shares, or emails during a specific period. I chose this API because it provides structured article information that can be easily processed and analyzed using R.
My main research question is:
Which sections of The New York Times have the most articles appearing on the most-viewed list during the past seven days?
I selected the New York Times Most Popular API, which provides information about the most-viewed articles over a specified period.
API Endpoint:
/svc/mostpopular/v2/viewed/7.json
API Base URL:
https://api.nytimes.com
Request Parameters:
period = 7: Retrieves articles from the seven-day
most-viewed list.api-key: Authenticates the API request.The API returns article information in JSON format, including article titles, sections, publication dates, and other metadata.
An API key is required to access the data. For security, I will store
my key in an environment variable and retrieve it in R using
Sys.getenv("NYT_API_KEY") instead of including it directly
in the code.
This approach protects the API key and allows the R code to be shared without exposing authentication credentials.
I plan to complete the assignment using the following steps:
Load R packages: Use httr, jsonlite, dplyr, and other tidyverse packages needed to retrieve and process the data.
Access the API key: Retrieve the stored API key using Sys.getenv(“NYT_API_KEY”).
Request data: Send a GET request to the New York Times Most Popular API.
Read the JSON response: Convert the returned JSON data into an R object.
Extract article information: Identify the results portion of the response, which contains the article records.
Create a tidy data frame: Convert the extracted information into a tibble.
Select useful variables: Keep columns such as article title, section, subsection, publication date, and article URL.
Analyze the results: Group articles by section and count how many articles appear in each category.
Present findings: Display a summary table showing which sections have the highest number of articles on the most-viewed list.
Before analyzing the data, I will review the returned information to identify any missing values, unnecessary columns, or formatting issues.
Some articles may not have a subsection or other optional information. I will keep these missing values as NA unless a specific adjustment is necessary.
The API response may also contain nested information, such as multimedia details. Since my analysis focuses on article sections, I will exclude these fields from the main data frame.
I will also convert publication dates into the correct R date format so they can be used in future analysis if needed.
Finally, I will select only the variables relevant to my research question. Each row in the final data frame will represent one article, and each column will represent a specific article attribute.
By completing this assignment, I expect to develop a reproducible R workflow that retrieves data from the New York Times Most Popular API and converts the JSON response into a clean, organized data frame.
I will then summarize the articles by news section to identify which sections appear most frequently among the most-viewed articles during the seven-day period.
This analysis will help me practice working with APIs, handling JSON data, cleaning datasets, and performing basic exploratory analysis using R.
The results will describe the distribution of articles in the API’s most-viewed list rather than measure the total readership of each news section.