Exercise 01 - dplyr – data wrangling

Author
Affiliations
John R Little

Duke University

Published

January 6, 2023

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.

  1. Add a code chunk and Load the tidyverse library package
library(tidyverse)
  1. Insert a code chunk and import (read_csv()) the brodhead practice data from the data directory. 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.

  1. Which restaurant has the lowest cost item and what is the item?
brodhead %>% 
  _______(cost) %>% 
  select(name, type, itemName, cost)
  1. Which restaurant has the most expensive item(s)? What are those item(s)?
brodhead %>% 
  ____(____(cost)) %>% 
  select(name, type, itemName, cost)
  1. At the Brodhead Center, how many of the entrees (found in the menuType variable) cost eight dollars?
brodhead %>% 
  ______(cost == _, menuType == "______") %>% 
  select(name, menuType, itemName, cost)
  1. 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)
  1. How many entrees are in the dataset (menuType variable)? How many desserts
brodhead %>% 
  _____(menuType)
# You can use `filter()` to limit by menuType

Answers

Answers can be found in the exercise_01_answers.Rmd file

Quiz

Take the interactive quiz