How can I find historical weather data for analysis in R?

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.

If you would like to follow along with a video as well, open a browser on the matching YouTube video here: https://www.youtube.com/watch?v=0eHbrIYhFzY

The estimated time to complete this exercise is about 5 minutes.

Step 1

mceclip1.png

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.

Step 2

mceclip0.png

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.

Step 3

blobid0.png

For this example, we’ll select the option to manually enter a location to use for our historical weather query.  However, we could instead load a sheet of addresses or paste in a list as plain text if we have a location list already available.

Step 4

blobid2.png

Instead of actually entering an address, we will let the system use our current location by clicking on the “Populate from your location” link below the location text box.  However, we could instead manually enter an address, a city name, or a postal code.  Optionally, we can give the location a friendly name so that we can reference it easily in the output data.  However, we typically don’t need this alternate name if we are only working with a single location as we are in this example.

Step 5

blobid0.png

Next, we need to choose the query type.  Since we are interested in weather reports from the past, we’ll select the Historical Data option.  However, in this panel there are other options as well such as weather forecast queries, historical data summary reports, and hourly data.  These are among the many options covered by our other tutorials and videos.

Step 6

blobid1.png

When we select the Historical Data option, the panel opens for us to set the parameters for an historical weather data query.  The primary option that we need to select is the Date Range.  Use the calendar selectors for the start and end points to select a range that you are interested in for your analysis.  The left-hand side calendar selects the starting date and the right-hand side calendar selects the ending date.  You may select a month, a few months, an entire year, or more depending upon your needs and account level.

Step 7

blobid2.png

When we run the query the default view is the weather calendar.  This view provides a simple overview of the result data.  It is very useful for comparing data from multiple locations side-by-side.

Step 8

blobid4.png

To see more details we can change to the grid view by clicking the “Raw Grid” button near the top of the page.  This view shows a single row for each day of the requested range and at the location we selected earlier.  You can now see the various weather metrics that are provided in the output data.  These include common values such as temperature, precipitation, and wind as well as less common value such as heat index, cloud cover, and wind gusts.  For more information on the details and how to use all of our weather metrics, see our weather data documentation.

Step 9

blobid5.png

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.

Step 10

blobid6.png

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.

Step 11

blobid7.png

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.

blobid9.png

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.

Leave a Reply

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