<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/integrating-data-from-weathercan/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ropensci.org/blog/2018/03/06/weathercan/"> rOpenSci - open tools for open science</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I love working with R and have been sharing the love with my friends and colleagues for almost seven years now. I’m one of those really annoying people whose response to most analysis-related questions is “You can do that in R! Five minutes, tops!” or “Three lines of code, I swear!” The problem was that I invariably spent an hour or more showing people how to get the data, load the data, clean the data, transform the data, and join the data, before we could even start the “five minute analysis”. With the advent of <a rel="nofollow" href="https://www.tidyverse.org/" target="_blank"><code>tidyverse</code></a>, data manipulation has gotten much, much easier, but I still find that data manipulation is where most new users get stuck. This is one of the reasons why, when I designed <a rel="nofollow" href="http://github.com/ropensci/weathercan" target="_blank"><code>weathercan</code></a>, I tried as hard as possible to make it simple and straightforward.</p>
<p><code>weathercan</code> is an R package designed to make it easy to access historical weather data from <a rel="nofollow" href="http://climate.weather.gc.ca/historical_data/search_historic_data_e.html" target="_blank">Environment and Climate Change Canada (ECCC)</a>. It downloads, combines, cleans, and transforms the data from multiple weather stations and across long time frames. So when you access ECCC data, you get everything in one dataset. Nifty, eh?</p>
<p>Although downloading data with <code>weathercan</code> is fairly straight forward, weather data often needs to be integrated into other data sets. You may want to combine <code>weathercan</code> data with other types of measurements (e.g., river water samples on a specific day), or summarize and join it with data on other scales (e.g. temporal or spatial). Depending on the other data this can be a tricky step. That’s why I’m going to walk you through some different ways of integrating weather data from <code>weathercan</code> with other data sets.</p>
<p>We’ll also be using several other R packages to do this, so why don’t we load them right now:</p>
<pre><code># Data manipulation and plotting
library(dplyr)
library(ggplot2)

# Checking data completeness
library(naniar)

# Access to data containing feeder visits by birds
library(feedr)

# Spatial analyses
library(sf)
library(mapview)
</code></pre>
<p>Well, I’ve told you it’s easy to get data from <code>weathercan</code>, so let’s start by doing so. For example, if you wanted to download weather data for all of Manitoba, Canada since the New Year, you have only three steps:</p>
<p>1) Load the package:</p>
<pre><code>library(weathercan)
</code></pre>
<p>2) Look at the built in <code>stations</code> data set to find the specific stations you’re interested in (you can also use the <code>stations_search()</code> function). Here, we’ll use the <a rel="nofollow" href="http://dplyr.tidyverse.org/" target="_blank"><code>dplyr</code></a> package (part of <a rel="nofollow" href="https://www.tidyverse.org/" target="_blank"><code>tidyverse</code></a>) to <code>filter()</code> stations to only those in the province of Manitoba, which record data at daily intervals, and which have an end date of 2018 or later (which likely means it’s still operational at the date of writing this post). Note that we’ll also be removing some columns (<code>prov</code>, <code>climate_id</code>, <code>WMO_id</code>, <code>TC_id</code>) just for clarity.</p>
<pre><code>mb &lt;- filter(stations, 
             prov == &quot;MB&quot;,
             interval == &quot;day&quot;,
             end >= 2018) %>%
  select(-prov, -climate_id, -WMO_id, -TC_id)
mb
</code></pre>
<pre><code>## # A tibble: 70 x 8
##    station_name         station_id   lat    lon  elev interval start   end
##    </code></pre>