/
exercise_01_answers.Rmd
128 lines (90 loc) · 2.55 KB
/
exercise_01_answers.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
---
title: "Answers 01 - dplyr -- data wrangling"
output: html_notebook
---
### Setup
a. Load the `tidyverse` library package
```{r message=FALSE, warning=FALSE}
library(tidyverse)
```
b. Import data
```{r message=FALSE, warning=FALSE}
brodhead <- read_csv("data/brodheadCenter.csv")
```
### exercise_01 -- Data Wrangling
1. Which restaurant has the lowest cost item and what is the item?
```{r}
brodhead %>%
arrange(cost) %>%
select(name, type, itemName, cost)
```
#### Alternative Answer
```{r}
brodhead %>%
filter(cost == min(cost)) %>%
select(name, type, itemType, cost)
```
2. Which restaurant has the most expensive item(s)? What are those item(s)?
```{r}
brodhead %>%
arrange(desc(cost)) %>%
select(name, type, itemName, cost)
```
#### Alternative Answers
```{r}
answer2 <- brodhead %>%
select(name, type, itemName, cost) %>%
filter(type == "restaurant",
cost == max(cost))
answer2
```
```{r}
expensive_items <- brodhead %>%
select(name, type, itemName, cost) %>%
filter(type == "restaurant",
cost == max(cost)) %>%
select(name, itemName)
expensive_items
```
**Example of inline coding** (Render report to see result.)
The `r unique(expensive_items$name)` restaurant offers the following items at the highest cost: `r expensive_items$itemName`.
3. At the Brodhead Center, how many of the entrees (found in the `menuType` variable) cost eight dollars?
```{r}
brodhead %>%
filter(cost == 8, menuType == "entree") %>%
select(name, menuType, itemName, cost)
```
The **answer** is 8. You know this because the Tibble (data frame) has 8 rows.
#### Alternative Answer
```{r}
brodhead %>%
filter(cost == 8, menuType == "entree") %>%
select(name, menuType, itemName, cost) %>%
count(menuType)
```
#### Alternative Answer
```{r}
eight_dollar_items <- brodhead %>%
filter(cost == 8, menuType == "entree") %>%
select(name, menuType, itemName, cost)
length(eight_dollar_items$itemName)
```
4. 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.
```{r}
brodhead %>%
mutate(halfPrice = (cost / 2)) %>%
select(name, itemName, cost, halfPrice)
```
5. How many entrees are in the dataset (`menuType` variable)? How many desserts
```{r}
brodhead %>%
count(menuType)
# You can use `filter()` to limit by menuType
```
#### Alternative Answer
```{r}
brodhead %>%
count(menuType) %>%
filter(menuType == "entree") %>%
pull(n)
```