R

How to use OpenWeather API in R

Here is an approach that doesn’t require a dedicated package for using OpenWeather API in R.

First of all, you should go to OpenWeather to register, choose what do you want, and get an API key. For some data wrangling experiments, I chose free current weather and forecast collection. The API key was available in my profile API keys tab.

For the Free plan, here are important API call instructions. For example, you will need geographical coordinates (latitude, longitude).

I will go with the hardest one – historical weather data. To get historical data for the last five days, you need to make five API calls (one call for each day). In this scenario, you will need a for loop in R to collect all the data. If you have a list of coordinates, then that is another for loop, but keep in mind limitations for your plan.

Historical weather data in R

Below is how to retrieve historical weather data from yesterday. OpenWeather API call should contain DateTime in numeric form.

The result contains a nested list with hourly data and weather description.

If you want to convert it to the data frame, then below is how to do that only for hourly or all available data. In more challenging cases with the nested list in R, take a look at this post.

 

Multiple weather events in the same time slot

In rare cases, there are multiple weather events in the same time slot.

Each of them will make a new row, and you might be interested to combine them like in this post.

Simple for loop in R to collect OpenWeather API historical weather data

Let’s say you want to collect historical weather data for the last 5 days and only a few of the available columns. First of all, there will be an empty R data frame that defines columns that are needed. It is necessary to make for loop simple. After this loop, there is code that combines multiple weather events in the same time slot in case there is any.

Here is a sample from selected hourly data collection.



0 comments on “How to use OpenWeather API in R

Leave a Reply

Your email address will not be published. Required fields are marked *