-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-Iterate.rmd
156 lines (110 loc) · 3.69 KB
/
06-Iterate.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
---
title: "Iteration"
output: html_document
---
```{r setup}
library(tidyverse)
# Toy data
set.seed(1000)
exams <- list(
student1 = round(runif(10, 50, 100)),
student2 = round(runif(10, 50, 100)),
student3 = round(runif(10, 50, 100)),
student4 = round(runif(10, 50, 100)),
student5 = round(runif(10, 50, 100))
)
extra_credit <- list(0, 0, 10, 10, 15)
```
## Your Turn 1
What kind of object is `mod`? Why are models stored as this kind of object?
mod is a "lm" object. Models are stored as lm's because they are linear models.
```{r}
mod <- lm(price ~ carat + cut + color + clarity, data = diamonds)
## View(mod)
class(mod)
```
## Consider
What's the difference between a list and an **atomic** vector?
Lists are vertical and can hold objects of different data types, as well as other lists. Atomic vectors are horizontal and one atomic vectors can only hold objects of the same data type, and cannot hold other atomic vectors.
Atomic vectors are: "logical", "integer", "numeric" (synonym "double"), "complex", "character" and "raw" vectors.
## Your Turn 2
Here is a list:
```{r}
a_list <- list(nums = c(8, 9),
log = TRUE,
cha = c("a", "b", "c"))
```
Here are two subsetting commands. Do they return the same values? Run the code chunk above, _and then_ run the code chunks below to confirm
a_list["nums"] returns a list that contains an atomic vector, while a_list$nums returns an atomic vector (no list).
```{r}
a_list["nums"]
```
```{r}
a_list$nums
```
## Your Turn 3
What will each of these return? Run the code chunks to confirm.
These run a vector and an error message.
```{r}
vec <- c(-2, -1, 0, 1, 2)
abs(vec)
```
```{r, error = TRUE}
lst <- list(-2, -1, 0, 1, 2)
abs(lst)
```
## Your Turn 4
Run the code in the chunks. What does it return?
This code returns a list.
```{r}
list(student1 = mean(exams$student1),
student2 = mean(exams$student2),
student3 = mean(exams$student3),
student4 = mean(exams$student4),
student5 = mean(exams$student5))
```
```{r}
library(purrr)
map(exams, mean)
```
## Your Turn 5
Calculate the variance (`var()`) of each student’s exam grades.
```{r}
exams %>%
map(var)
```
## Your Turn 6
Calculate the max grade (`max()`)for each student. Return the result as a vector.
```{r}
exams %>%
map_dbl(max)
```
## Your Turn 7
Write a function that counts the best exam twice and then takes the average. Use it to grade all of the students.
1. Write code that solves the problem for a real object
2. Wrap the code in `function(){}` to save it
3. Add the name of the real object as the function argument
```{r}
vec <- exams[[1]]
grade <- function(vec) {
(sum(vec) - min(vec)) / (length(vec) - 1)
}
exams %>%
map_dbl(grade)
```
### Your Turn 8
Compute a final grade for each student, where the final grade is the average test score plus any `extra_credit` assigned to the student. Return the results as a double (i.e. numeric) vector.
```{r}
exams %>%
map2_dbl(extra_credit, function(x, y) mean(x) + y)
```
***
# Take Aways
Lists are a useful way to organize data, but you need to arrange manually for functions to iterate over the elements of a list.
You can do this with the `map()` family of functions in the purrr package.
To write a function,
1. Write code that solves the problem for a real object
2. Wrap the code in `function(){}` to save it
3. Add the name of the real object as the function argument
This sequence will help prevent bugs in your code (and reduce the time you spend correcting bugs).
<!-- This file by Charlotte Wickham is licensed under a Creative Commons Attribution 4.0 International License, adapted from the orignal work at https://github.com/rstudio/master-the-tidyverse by RStudio. -->