Use R To Pull Energy Data From The Department of Energy’s EIA API

Guest Blog Last Updated : 14 Jan, 2021
5 min read

Introduction

API Access — API Key

API Access — API Key

registration form

Finding Data Sets

Finding datasets

EAI API data details

Obtaining the Series ID

API query browser

Source: EIA

Source: EIA

Pulling in Data Using R

#Import libraries
install.packages(c("httr", "jsonlite"))library(httr)
library(jsonlite)
# API Key from EIA
key <- 'PASTE YOUR API KEY HERE'# Paste your Series IDs in the list, separated by commas
padd_key <- list('PET.MCRRIP12.M','PET.MCRRIP22.M',
                 'PET.MCRRIP32.M','PET.MCRRIP42.M',
                 'PET.MCRRIP52.M')# Choose the start and end dates
startdate <- "2010-01-01" #YYYY-MM-DD
enddate <- "2020-01-01" #YYYY-MM-DD

Image for post

j = 0
for (i in padd_key) {url <- paste('http://api.eia.gov/series/api_key=',key,'&series_id=',i,sep="")  # Make the call to the EIA's API
  res <- GET(url)
  json_data <- fromJSON(rawToChar(res$content))
  data <- data.frame(json_data$series$data)
  data$Year <- substr(data$X1,1,4)
  data$Month <- substr(data$X1,5,6)
  data$Day <- 1# Create date format
  data$Date <- as.Date(paste(data$Year, data$Month, data$Day,    sep='')) # Rename the column to its given name from the EIA
  colnames(data)[2]  <- json_data$series$name # Drop the unnecessary date columns
  data <- data[-c(1,3,4,5)]
  
  if (j == 0){
    data_final <- data
  }
  else{
    data_final <- merge(data_final,data,by="Date")
  }
  
  j = j + 1
}# Splice the data between the start and end dates
data_final <- subset(data_final, Date >= startdate & Date <= enddate)
Data frame in R of EIA’s API pull

Data frame in R of EIA’s API pull

 

About the Author

Author

Shu Lee

Shu is interested in using data to develop insights, solve real-world problems, and drive business decisions. She is passionate about data science, machine learning, economics, and statistics, and continues to learn every day.

 

The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details