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.
require(jsonlite) require(dplyr) require(tidyr) lat = 56.9496 lon = 24.1052 # this must be as.numeric from datetime my_dt = as.numeric(as.POSIXct(strftime(Sys.Date()-0))) API_key = "xxxxx" url <- paste0("http://api.openweathermap.org/data/2.5/onecall/timemachine?lat=",lat , "&lon=", lon , "&dt=", my_dt , "&appid=", API_key) ow <- fromJSON(url) # only historical hourly data hourly <- ow$hourly hourly$dt <- as.POSIXct(hourly$dt, origin = "1970-01-01", tz = 'UTC') hourly <- hourly %>% unnest(weather) # all data all_data <- as.data.frame(ow) all_data$hourly.dt <- as.POSIXct(all_data$hourly.dt, origin = "1970-01-01", tz = 'UTC') all_data <- all_data %>% unnest(hourly.weather)
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.
# historical data collection require(jsonlite) require(dplyr) require(tidyr) # empty data frame to start collecting the data hourly_comb <- data.frame(matrix(ncol = 3, nrow = 0)) names(hourly_comb) <- c("dt", "main", "description") lat = 56.9496 lon = 24.1052 # this must be as.numeric from datetime my_dt = as.numeric(as.POSIXct(strftime(Sys.Date()-0))) API_key = "xxxxx" # for loop from 0 (yesterday) to 4 (5 days ago) for (i in 0:4) { # changing datetime new_dt <- as.numeric(my_dt - i * 86400) url <- paste0("http://api.openweathermap.org/data/2.5/onecall/timemachine?lat=",lat , "&lon=", lon , "&dt=", new_dt , "&appid=", API_key) ow <- fromJSON(url) # only historical hourly data hourly <- ow$hourly hourly$dt <- as.POSIXct(hourly$dt, origin = "1970-01-01", tz = 'UTC') hourly <- hourly %>% unnest(weather) hourly <- hourly[c("dt", "main", "description")] # combination of all hourly data hourly_comb <- rbind(hourly_comb, hourly) } # in case there is two events in same dt hourly_comb <- hourly_comb %>% group_by(dt) %>% mutate( main = paste(main, collapse = " | "), description = paste(description, collapse = " | ") ) %>% distinct(dt, .keep_all = T)
Leave a Reply