In this article, I’m going to show you how to easily load historical weather data into your R analysis. We’ll walk through the process step-by-step showing you how to construct the query in Visual Crossing Weather and then use the query URL to import the weather history results using R Studio.
Also see our companion article about importing weather forecast data into an R analysis.
The estimated time to complete this exercise is about 5 minutes.
We’ll start by going to the Visual Crossing Weather Data page. We then need to click on the link to go to weather data download page near the top of the page.
Once on the log-in page, you will need to sign into your Visual Crossing Weather account. If you don’t already have an account simply click on the orange button on the right-hand side of the login box. Your free trial account will give you instant access to historical weather data for any location around the globe.
Enter the location of interest and the time frame you want to query.
We could now download the data as a CSV by pressing the “Download all data” button for import into an analysis tool, but instead, we’ll switch to the API view to generate the query URL. Using a query URL will allow us to directly import this data into our R code. In addition, this query will allow R to fetch live data and refresh the data as needed. Since the default query URL output is CSV, which R can read directly, we can simply press the “Copy full query” button to copy the query URL to the clipboard.
We can now use this URL to load the weather query results directly into R. For this example, we’ll switch to RStudio. Loading the live result data is as simple as entering the URL into R’s read.csv() function. To do so, we would enter a command like the following:
HistoryData <- read.csv(“<URL>”)
Where the “<URL>” is the value the we copied from the Visual Crossing Weather API page.
Once loaded, we can view the table in RStudio either by clicking on it or by manually typing the View() command specifying the name of the table that we used above. Statistical analysis, plotting, correlating, reporting and all of the other strengths of R can now be applied directly to our historical weather data. In more advanced applications we could adjust the query URL from our code to dynamic change the parameters of the query itself.
If you would like to learn more about using Visual Crossing Weather options such as weather forecasts, multiple location import, and use within other analysis tools please see our other tutorials.