library(tidyverse)
library(scales)
Lab 3 - Data tidying
Learning goals
In this lab, you will…
- use pivoting to reshape data
- use joins to bring together two datasets
- continue developing a workflow for reproducible data analysis
- continue working with data visualization tools
Getting started
- Go to the sta199-fa22-01 organization on GitHub. Click on the repo with the prefix
lab-03
. It contains the starter documents you need to complete the lab. - Clone the repo and start a new project in RStudio. See the Lab 0 instructions for details on cloning a repo and starting a new R project.
- First, open the Quarto document
lab-03.qmd
and Render it. - Make sure it compiles without errors.
Warm up
Before we introduce the data, let’s warm up with some simple exercises.
- Update the YAML, changing the author name to your name, and render the document.
- Commit your changes with a meaningful commit message.
- Push your changes to GitHub.
- Go to your repo on GitHub and confirm that your changes are visible in your `
.qmd
and .pdf
files. If anything is missing, render, commit, and push again.
Packages
We’ll use the tidyverse package for much of the data wrangling and the scales package for better plot labels. These packages are already installed for you. You can load it by running the following in your Console:
Data
The datasets that you will work with in this dataset come from the Organization for Economic Co-Operation and Development (OECD), stats.oecd.org.
Part 1: Inflation across the world
For this part of the analysis you will work with inflation data from various countries in the world over the last 30 years.
<- read_csv("data/country-inflation.csv") country_inflation
What does each row of the
country_inflation
dataset represent? What are the columns in the dataset and what do they represent?Reshape (pivot)
country_inflation
such that each row represents a country/year combination, with columnscountry
,year
, andannual_inflation
. Make sure thatannual_inflation
is a numeric variable. Save the result as a new data frame – you should give it a concise and informative name.Create a vector called
countries_of_interest
which contains the names of countries you want to visualize the inflation rates for over the years. For example, if these countries are Türkiye and United States, you can express this as follows:<- c("Türkiye", "United States") countries_of_interest
Your
countries_of_interest
should consist of no more than five countries. Make sure that the spelling of your countries matches how they appear in the dataset.In a single pipeline, filter your reshaped dataset to include only the
countries_of_interest
from the previous exercise and create a plot of annual inflation vs. year for these countries. Then, in a few sentences, state why you chose these countries and describe the patterns you observe in the plot, particularly focusing on anything you find surprising or not surprising, based on your knowledge (or lack thereof) of these countries economies.Data should be represented with points as well as lines connecting the points for each country.
Each country should be represented by a different color line.
Axes and legend should be properly labeled.
The plot should have an appropriate title (and optionally a subtitle).
Axis labels for annual inflation should be shown in percentages (e.g., 25% not 25). Hint: The
label_percent()
function from the scales package will be useful.ggplot(...) + + ... scale_y_continuous(label = label_percent(scale = 1))
If you haven’t yet done so, now is a good time to render, commit, and push. Make sure that you commit and push all changed documents and your Git pane is completely empty before proceeding.
Part 2: Inflation in the US
The OECD defines inflation as follows:
Inflation is a rise in the general level of prices of goods and services that households acquire for the purpose of consumption in an economy over a period of time.
The main measure of inflation is the annual inflation rate which is the movement of the Consumer Price Index (CPI) from one month/period to the same month/period of the previous year expressed as percentage over time.
Source: OECD CPI FAQ
CPI is broken down into 12 divisions such as food, housing, health, etc. Your goal in this part is to create another time series plot of annual inflation, this time for US only.
The data you will need to create this visualization is spread across two files:
us-inflation.csv
: Annual inflation rate for the US for 12 CPI divisions. Each division is identified by an ID number.cpi-divisions.csv
: A “lookup table” of CPI division ID numbers and their descriptions.
Let’s load both of these files.
<- read_csv("data/us-inflation.csv")
us_inflation <- read_csv("data/cpi-divisions.csv") cpi_divisions
- Add a column to the
us_inflation
dataset calleddescription
which has the CPI division description that matches thecpi_division_id
, by joining the two datasets.Determine which type of join is the most appropriate one and use that.
Note that the two datasets don’t have a common variable. Review the help for the join functions to determine how to use the
by
argument when the names of the variables that the datasets should be joined by are different.
- Create a vector called
divisions_of_interest
which contains the descriptions or IDs of CPI divisions you want to visualize. Yourdivisions_of_interest
should consist of no more than five divisions. If you’re using descriptions, make sure that the spelling of your divisions matches how they appear in the dataset. - In a single pipeline, filter your joined dataset to include only the
divisions_of_interest
from the previous exercise and create a plot of annual inflation vs. year for these divisions. Then, in a few sentences, state why you chose these divisions and describe the patterns you observe in the plot, particularly focusing on anything you find surprising or not surprising, based on your knowledge (or lack thereof) of inflation rates in the US over the last decade.Data should be represented with points as well as lines connecting the points for each division.
Each division should be represented by a different color.
Axes and legend should be properly labeled.
If your legend has labels that are too long, you can try moving the legend to the bottom and stack the labels vertically. Hint: The
legend.position
andlegend.direction
arguments of thetheme()
functions will be useful.ggplot(...) + + ... theme( legend.position = "bottom", legend.direction = "vertical" )
The plot should have an appropriate title (and optionally a subtitle).
Axis labels for annual inflation should be shown in percentages (e.g., 25% not 25).
If you haven’t yet done so since Part 1, now is a good time to render, commit, and push. Make sure that you commit and push all changed documents and your Git pane is completely empty before proceeding.
Submission
Once you are finished with the lab, you will your final PDF document to Gradescope.
To submit your assignment:
- Go to http://www.gradescope.com and click Log in in the top right corner.
- Click School Credentials \(\rightarrow\) Duke NetID and log in using your NetID credentials.
- Click on your STA 199 course.
- Click on the assignment, and you’ll be prompted to submit it.
- Mark all the pages associated with exercise. All the pages of your lab should be associated with at least one question (i.e., should be “checked”). If you do not do this, you will be subject to lose points on the assignment.
- Select the first page of your .pdf submission to be associated with the “Workflow & formatting” question.
Grading
Component | Points |
---|---|
Ex 1 | 3 |
Ex 2 | 6 |
Ex 3 | 4 |
Ex 4 | 12 |
Ex 5 | 4 |
Ex 6 | 4 |
Ex 7 | 12 |
Workflow & formatting | 5 |
Total | 50 |