Exercise 2: Tidyverse
Setting up
Create new Quarto document. For working on the exercise, create a new Quarto document with a descriptive name and save it where you can find it again, i.e. in the folder where you downloaded the teaching material. You can use the commands shown in presentation2.qmd to solve this exercise. There is no shame in outright copying from the presentation2.qmd script, provided you understand what the command is doing.
Load packages. You will need to load the packages
tidyverse
andreadxl
for this exercise.
Importing data and a first look at the dataset
The data set used in these exercises was compiled from data downloaded from the website of the UKβs national weather service, the Met Office. It is saved in the file climate.xlsx1 which can be found in the folder Exercises/Data/
. The spreadsheet contains monthly data from five UK weather stations for the following variables:
Variable name | Explanation |
---|---|
station | Location of weather station |
year | Year |
month | Month |
af | Days of air frost |
rain | Rainfall in mm |
sun | Sunshine duration in hours |
device | Brand of sunshine recorder / sensor |
Load data. Start by importing the dataset using either the
read_excel()
function or theImport Dataset
button and name itclimate
. If you load withImport Dataset
it is a good idea to copy the command into your script so that the next time you run your script you can just execute that line instead of having to find the file again.First look at data. Write the name of the dataframe, i.e.
climate
, into the console and press enter to see the first rows of the dataset. You can also click on theclimate
object in the Environment panel.Explore your dataset and understand what data you have.
How many observations, i.e. rows are there?
How many data columns are there and what are their types?
What is the information in each row and column?
How many different stations are there?
How many rows per station?
Working with the data
Before you proceed with the exercises in this document, make sure you load the tidyverse
in order to use the functions from this package.
Count the number of rows that did not have any days with air frost.
Count the number of rows per station that did not have any days with air frost.
Select from the climate dataset (remember to
filter
rows andselect
columns):all rows from the station in Oxford
all rows from the station in Oxford when there were at least 100 hours of sunlight
all rows from the stations in Oxford and Camborne when there were at least 100 hours of sunlight
a subset that only contains the
station
,year
andrain
columns
The next few questions build on each other, each adding a piece of code:
Compute the average rainfall over the full dataset by using the
summarize
function. You can look at the examples we did at the end of presentation 2.Now, compute the average rainfall, standard deviation of the rainfall and the total rainfall (the sum) on the full dataset. I.e. all three measures should be inside the same resulting table. Have a look at the tidyverse lecture if you have trouble with this.
Now, use
group_by
beforesummarize
in order to compute group summary statistics (average, standard deviation, and sum) but split up into each of the five weather stations.Include a column in the summary statistics which shows how many observations, i.e. rows, the data set contains for each station.
Sort the rows in the output in descending order according to average annual rainfall.
Manipulating the data
Create a new column in
climate
and save the new dataset in a different variable so you donβt overwrite your originalclimate
data. The new column should count the number of days in each month without air frost, based on the existingaf
column. For this exercise, assume each month has 30 days. To find the number of days without air frost, subtract the value in theaf
column from 30.Add another column to your new dataset that says whether the weather this month was good. We consider a month to be good if it had at least 100 hours of sunshine and less than 100 mm of rain. Otherwise the weather was bad.
How many months are there with good weather (use the column you made in 14) for each station? Find the station that has the most months with good weather.
Complex operations
The final questions require that you combine commands and variables of the type above.
For each weather station apart from the one in Armagh, compute the total rainfall and sunshine duration for months that had no days of air frost. Present the totals in centimetres and days, respectively.
Identify the weather station for which the median number of monthly sunshine hours over the months April to September was largest.
Wrapping up
- Like in the last exercise; imagine you need to send your code to a collaborator. Review your code to ensure it is clear and well-structured, so your collaborator can easily understand and follow your work. Render your Quarto document and look at the result.
Footnotes
Contains public sector information licensed under the Open Government Licence v3.0.β©οΈ