100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Summary

Summary DSCI Tutorial 3 - tutorial_wrangling_solution (2022)

Rating
-
Sold
1
Pages
12
Uploaded on
11-04-2022
Written in
2021/2022

Solutions to DSCI 100 tutorial 3 - tutorial_wrangling










Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
April 11, 2022
Number of pages
12
Written in
2021/2022
Type
Summary

Subjects

Content preview

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.

F. Labels the x-axis.

Functions

1. group_by
2. map
3. read_tsv
4. summarise
5. xlab
6. pivot_longer

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.




image source: https://media.giphy.com/media/8p3ylHVA2ZOIo/giphy.gif (https://media.giphy.com/media/8p3ylHVA2ZOIo/giphy.gif)

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

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
travissmith1 UBC
View profile
Follow You need to be logged in order to follow users or courses
Sold
97
Member since
4 year
Number of followers
61
Documents
36
Last sold
1 month ago

3.6

16 reviews

5
6
4
6
3
0
2
0
1
4

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions