library(tidyverse)Exercise 01 - dplyr – data wrangling
Goals:
Use dplyr functions to wrangle data
In this exercise you’ll practice the dplyr verbs (i.e. functions) mentioned in the demonstration.
arrange()filter()mutate()select()count()
Directions
Setup
We’ve prebuilt a data set that has information about menus in Duke’s Brodhead Center (dining hall) from three restaurants. Create a new R Notebook, load the tidyverse package, and load the dining hall dataset, as a tibble. The broadhead dataset is a CSV file in the data directory.
- Add a code chunk and Load the
tidyverselibrary package
- Insert a code chunk and import (
read_csv()) the brodhead practice data from thedatadirectory. Assign the imported data to an object name, e.g. “brodhead”
brodhead <- read_csv("../data/brodheadCenter.csv")Now you are ready to make new code chunks and follow the steps in Exercise 1 (below).
Answers can be found in the exercise_01_answers.Rmd file
Exercise: Data Wrangling
All of the following questions are based on the sample of restaurants represented in the dataset which is accurate as of September 2, 2016.
- Which restaurant has the lowest cost item and what is the item?
brodhead %>%
_______(cost) %>%
select(name, type, itemName, cost)- Which restaurant has the most expensive item(s)? What are those item(s)?
brodhead %>%
____(____(cost)) %>%
select(name, type, itemName, cost)- At the Brodhead Center, how many of the entrees (found in the
menuTypevariable) cost eight dollars?
brodhead %>%
______(cost == _, menuType == "______") %>%
select(name, menuType, itemName, cost)- The head of Duke dining is considering reducing prices at the Brodhead Center. Using what we’ve learned in class, write code that will calculate a new variable (
halfPrice) that contains items at half price.
brodhead %>%
______(halfPrice = (____ / 2)) %>%
select(name, itemName, cost, halfPrice)- How many entrees are in the dataset (
menuTypevariable)? How many desserts
brodhead %>%
_____(menuType)
# You can use `filter()` to limit by menuTypeAnswers
Answers can be found in the exercise_01_answers.Rmd file
Quiz
Take the interactive quiz