Tutorial 3: Cleaning and Wrangling Data
Lecture and Tutorial Learning Goals:
After completing this week's lecture and tutorial work, you will be able to:
define the term "tidy data"
discuss the advantages and disadvantages of storing data in a tidy data format
recall and use the following tidyverse functions and operators for their intended data wrangling tasks:
select
filter
%>%
map
mutate
summarize
group_by
pivot_longer
pivot_wider
%in%
Any place you see ... , you must fill in the function, variable, or data to complete the code. Replace fail() with your completed code and run the
cell!
In [ ]:
### Run this cell before continuing.
library(repr)
library(tidyverse)
source("tests.R")
source("cleanup.R")
options(repr.matrix.max.rows = 6) #limits output of dataframes to 6 rows
Question 0.1
{points: 1}
Match the following definitions with the corresponding functions used in R:
A. Transforms the input by applying a function to each element and returning a vector the same length as the input.
B. Reads files that have columns separated by tabs.
C. Most data operations are done on groups defined by variables. This function takes an existing data set and converts it into a grouped data set where
operations are performed "by group".
D. Works in an analogous way to mutate, except instead of adding columns to an existing data frame, it creates a new data frame.
E. "lengthens" data, increasing the number of rows and decreasing the number of columns.
For every description, create an object using the letter associated with the definition and assign it to the corresponding number from the list of functions.
For example:
A <- 1
B <- 2
C <- 3
...
F <- 6
, In [ ]:
# Replace the fail() with your answer.
### BEGIN SOLUTION
C <- 1
A <- 2
B <- 3
D <- 4
F <- 5
E <- 6
### END SOLUTION
In [ ]:
test_0.1()
1. Historical Data on Avocado Prices
In the tutorial, we will be finishing off our analysis of the avocado data set.
You might recall from the lecture that millennials LOVE avocado toast. However, avocados are expensive and this is costing millennials a lot more than
you think (joking again , well mostly...). To ensure that they can save enough to buy a house, it would be beneficial for an avocado fanatic to move to a
city with low avocado prices. From Worksheet 3 we saw that the price of the avocados is less in the months between December and May, but we still don't
know which region contains the cheapest avocados.
As a reminder, here are some relevant columns in the dataset:
average_price - The average price of a single avocado.
type - conventional or organic
year - The year
region - The city or region of the observation
small_hass_volume
large_hass_volume
extra_l_hass_volume
Additionally, the last three columns can be used to calculate total_volume in pounds (lbs). The goal for today is to find the region with the cheapest
avocados and then produce a plot of the total number of avocados sold against the average price per avocado (in US dollars) for that region. To do this,
you will follow the steps below.
1. use a tidyverse read_* function to load the csv file into your notebook
2. use group_by + summarize to find the region with the cheapest avocados.
3. use filter to specifically look at data from the region of interest.
4. use mutate to add up the volume for all types of avocados (small, large, and extra)
5. use ggplot to create our plot of volume vs average price
Question 1.1
{points: 1}
Read the file avocado_prices.csv found in the tutorial_03 directory using a relative path.
Assign your answer to an object called avocado .
In [ ]:
### BEGIN SOLUTION
avocado <- read_csv("data/avocado_prices.csv")
### END SOLUTION
avocado
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller travissmith1. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $7.49. You're not tied to anything after your purchase.