-
Notifications
You must be signed in to change notification settings - Fork 13
/
08-Iterate.Rmd
executable file
·153 lines (96 loc) · 3.05 KB
/
08-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
---
title: "Iterate"
output: html_notebook
editor_options:
chunk_output_type: inline
---
<!-- This file by Jake Thompson 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. -->
```{r setup, include = FALSE}
library(tidyverse)
# Toy data
set.seed(9416)
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?
```{r}
mod <- lm(price ~ carat + cut + color + clarity, data = diamonds)
View(mod)
```
## Quiz
What's the difference between a list and an **atomic** vector?
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
```{r}
a_list["nums"]
```
```{r}
a_list$nums
```
## Your Turn 3
What will each of these return? Run the code chunks to confirm.
```{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?
```{r}
list(student1 = mean(exams$student1),
student2 = mean(exams$student2),
student3 = mean(exams$student3),
student4 = mean(exams$student4),
student5 = mean(exams$student5))
```
```{r}
map(exams, mean)
```
## Your Turn 5
Calculate the variance (`var()`) of each student’s exam grades.
```{r}
exams
```
## Your Turn 6
Calculate the max grade (`max()`)for each student. Return the result as a vector.
```{r}
exams
```
## 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]]
```
## 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}
```
***
# 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).