Finding Weather Insights: Connecting to the OpenWeather API in R
The OpenWeather API (OWA) serves as a goldmine for weather enthusiasts and data analysts alike. This free-to-use API grants access to a rich tapestry of weather data, encompassing everything from current temperature and humidity to detailed forecasts. Whether you’re seeking to understand seasonal trends or gauge the impact of weather on customer behavior, the OpenWeather API empowers you to integrate valuable weather insights into your R projects.
The API offers data in three primary modes:
- Current Weather: Provides real-time snapshots of weather conditions, including temperature, humidity, pressure, wind speed, and more.
- Weather Forecast: Delivers predictions for upcoming days, allowing you to anticipate future weather patterns.
- Weather data for any timestamp: you can get historical weather data for the last 40+ years in addition to 4 days forecast.
It is worth to mention that OpenWeather provides access to additional information other than the Openweather API such as Air Pollution API, Fire Weather Index API, Weather Stations, and many more.
Enriching Your Datasets with Weather Data:
Incorporating weather data from public datasets source like OpenWeather into your R analyses can unlock new dimensions of understanding. Imagine studying retail sales data – by including weather information, you could uncover how weather fluctuations influence customer purchasing patterns. Perhaps colder temperatures lead to a surge in demand for hot beverages, while sunny days see a rise in ice cream sales. This knowledge can empower businesses to optimize inventory management and marketing strategies based on weather forecasts.
OpenWeather API Free Access
OpenWeather API offers different paid subscriptions that gives you access to weather data on current and forecast basis, on a free subscription you can only get current weather data and 5 Days with 3 Hour Forecast. Free Subscriptions are limited to 1000 API calls per day.
Prerequisites for Connecting OpenWeather API in R
Before diving into the code, ensure you have the following resources at your disposal:
- R: Download and install R from the official website (https://www.r-project.org/)
- RStudio: An integrated development environment that enhances your R workflow (https://posit.co/)
- R Packages: Install the
tidyverse
andhttr
packages using theinstall.packages()
function within R. These packages provide tools for data manipulation and communication with web APIs, respectively. - OpenWeather API Key: Obtain a free API key by registering on the OpenWeatherMap website (https://openweathermap.org/api), please note that once you generate a new free API key it could take couple of hours for the key to get activated before you can start using it.
Connecting to the OpenWeather API in R:
Now, let’s leverage R to tap into the power of the OpenWeather API. We’ll explore two methods for retrieving weather data:
1. Current Weather by City Name:
We will use Seoul as an example.
# Load required libraries
library(tidyverse)
library(httr)
# URL for Current Weather API
current_weather_url <- "https://api.openweathermap.org/data/2.5/weather"
# need to be replaced by your real API key
your_api_key <- "API Key"
# Input `q` is the city name
# Input `appid` is your API KEY,
# Input `units` are preferred units such as Metric or Imperial
current_query <- list(q = "Seoul", appid = your_api_key, units = "metric")
response <- GET(current_weather_url, query = current_query)
http_type(response)
Once you run the above code by adding your own API key and choosing your selected city; you should get below response:
## [1] "application/json"
Now using JSON to read the response and find the class of the data saved.
json_result <- content(response, as = "parsed")
class(json_result)
## [1] "list"
Now we will read the list to see the type of data available, it should contain very detailed weather data about the city of Seoul as per the output example seen below.
json_result
## $coord
## $coord$lon
## [1] 126.9778
##
## $coord$lat
## [1] 37.5683
##
##
## $weather
## $weather[[1]]
## $weather[[1]]$id
## [1] 804
##
## $weather[[1]]$main
## [1] "Clouds"
##
## $weather[[1]]$description
## [1] "overcast clouds"
##
## $weather[[1]]$icon
## [1] "04n"
##
##
##
## $base
## [1] "stations"
##
## $main
## $main$temp
## [1] 22.43
##
## $main$feels_like
## [1] 23.05
##
## $main$temp_min
## [1] 21.69
##
## $main$temp_max
## [1] 23.66
##
## $main$pressure
## [1] 1009
##
## $main$humidity
## [1] 89
##
##
## $visibility
## [1] 10000
##
## $wind
## $wind$speed
## [1] 3.09
##
## $wind$deg
## [1] 360
##
##
## $clouds
## $clouds$all
## [1] 100
##
##
## $dt
## [1] 1693167925
##
## $sys
## $sys$type
## [1] 1
##
## $sys$id
## [1] 8105
##
## $sys$country
## [1] "KR"
##
## $sys$sunrise
## [1] 1693169908
##
## $sys$sunset
## [1] 1693217345
##
##
## $timezone
## [1] 32400
##
## $id
## [1] 1835848
##
## $name
## [1] "Seoul"
##
## $cod
## [1] 200
To convert the named list to a data frame so that we can use data frame operations to process the data, we will use the below code. The code will collect all the details found in the JSON result output.
If you don’t need the complete data, you can create vectors for the data that you only need.
# Create some empty vectors to hold data temporarily
weather <- c()
visibility <- c()
temp <- c()
temp_min <- c()
temp_max <- c()
pressure <- c()
humidity <- c()
wind_speed <- c()
wind_deg <- c()
Then we will use below code to assign each JSON element to a vector, again the below code will save the complete output, if you need to collect specific data such as temperature you can keep the only the specific lines.
# $weather is also a list with one element, its $main element indicates the weather status such as clear or rain
weather <- c(weather, json_result$weather[[1]]$main)
# Get Visibility
visibility <- c(visibility, json_result$visibility)
# Get current temperature
temp <- c(temp, json_result$main$temp)
# Get min temperature
temp_min <- c(temp_min, json_result$main$temp_min)
# Get max temperature
temp_max <- c(temp_max, json_result$main$temp_max)
# Get pressure
pressure <- c(pressure, json_result$main$pressure)
# Get humidity
humidity <- c(humidity, json_result$main$humidity)
# Get wind speed
wind_speed <- c(wind_speed, json_result$wind$speed)
# Get wind direction
wind_deg <- c(wind_deg, json_result$wind$deg)
Finally to combine all vectors into one data frame, you can use the below code.
# Combine all vectors
weather_data_frame <- data.frame(
weather = weather,
visibility = visibility,
temp = temp,
temp_min = temp_min,
temp_max = temp_max,
pressure = pressure,
humidity = humidity,
wind_speed = wind_speed,
wind_deg = wind_deg
)
Now you can perform any necessary data transformation to prepare your data frame for data analysis.
2. Weather Forecast:
Forecasting is generally part of Data Science projects, you can forecast your product sales or inventory based on weather forecast if there is a strong relationship between weather and your products.
Being able to use OpenWeather API in R to collect Weather forecast is an important skill that you should learn, in this tutorial we will use the free OpenWeather API with R to collect 5 days forecasts for a list of cities.
Before we start writing the code to collect the data, we need to create vectors that will hold the data temporary, this is exactly the same vectors created in the previous tutorial but with the addition of the city name and timestamp.
# Create some empty vectors to hold data temporarily
# City name column
city <- c()
# Weather column, rainy or cloudy, etc
weather <- c()
# Sky visibility column
visibility <- c()
# Current temperature column
temp <- c()
# Max temperature column
temp_min <- c()
# Min temperature column
temp_max <- c()
# Pressure column
pressure <- c()
# Humidity column
humidity <- c()
# Wind speed column
wind_speed <- c()
# Wind direction column
wind_deg <- c()
# Forecast timestamp
forecast_datetime <- c()
Next the below code is used to collect the forecasted data and will work for all your projects regardless of the city name or the number of cities; as city names is not defined in this step.
# Get forecast data for a given city list
get_weather_forecaset_by_cities <- function(city_names) {
df <- data.frame()
for (city_name in city_names) {
# Forecast API URL
forecast_url <- "https://api.openweathermap.org/data/2.5/forecast"
# Create query parameters
forecast_query <- list(q = city_name, appid = "Your API key", units = "metric")
# Make HTTP GET call for the given city
forecast_response <- GET(forecast_url, query = forecast_query)
# Note that the 5-day forecast JSON result is a list of lists. You can print the response to check the results
forecast_json_list <- content(forecast_response, as = "parsed")
results <- forecast_json_list$list
result <- c(1:40)
# Loop the json result
for (x in result) {
city <- c(city, city_name)
# $weather is also a list with one element, its $main element indicates the weather status such as clear or rain
weather <- c(weather, results[[x]]$weather[[1]]$main)
# Get Visibility
visibility <- c(visibility, results[[x]]$visibility)
# Get current temperature
temp <- c(temp, results[[x]]$main$temp)
# Get min temperature
temp_min <- c(temp_min, results[[x]]$main$temp_min)
# Get max temperature
temp_max <- c(temp_max, results[[x]]$main$temp_max)
# Get pressure
pressure <- c(pressure, results[[x]]$main$pressure)
# Get humidity
humidity <- c(humidity, results[[x]]$main$humidity)
# Get wind speed
wind_speed <- c(wind_speed, results[[x]]$wind$speed)
# Get wind direction
wind_deg <- c(wind_deg, results[[x]]$wind$deg)
# Get forcast_datetime
forecast_datetime <- c(forecast_datetime, results[[x]]$dt_txt)
}
# Add the R Lists into a data frame
df <- data.frame(
city = city, weather = weather,
visibility = visibility,
temp = temp,
temp_min = temp_min,
temp_max = temp_max,
pressure = pressure,
humidity = humidity,
wind_speed = wind_speed,
wind_deg = wind_deg,
forecast_datetime
)
}
# Return a data frame
return(df)
}
Now it is time to define your cities and run the code to get the results saved into a data frame.
cities <- c("Seoul", "Washington, D.C.", "Paris", "Suzhou")
cities_weather_df <- get_weather_forecaset_by_cities(cities)
Now you can perform any necessary data transformation before starting your analysis, Or you can save the data frame to a CSV file.
# Write cities_weather_df to `cities_weather_forecast.csv`
write.csv(cities_weather_df, "raw_cities_weather_forecast.csv", row.names = FALSE)
This code was part of my capstone project for IBM Data Science with R certification, it was used for integrating weather data with Bike sharing system data in order to understand how weather and seasonality could influence bike rentals in major cities around the world, you can have a look at the full project at this Kaggle Notebook.
Conclusion:
By harnessing the OpenWeather API in R, you unlock a treasure trove of weather data, empowering you to enrich your R analysis with valuable insights. From understanding seasonal trends to uncovering weather’s influence on real-world phenomena, the possibilities are vast. So, unleash the power of weather data and elevate your R projects to new heights!