-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path120-iteration.Rmd
172 lines (124 loc) · 1.97 KB
/
120-iteration.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
157
158
159
160
161
162
163
164
165
166
167
168
# Iteration
## Broadcasting
```{r}
f_values <- c(0, 32, 212, -40)
```
```{r}
f_values * 10
```
```{r}
f_values * c(10, 100)
```
## For loops
Temp conversion functions
```{r}
f_k <- function(f_temp) {
((f_temp - 32) * (5 / 9)) + 273.15
}
k_c <- function(temp_k) {
if (is.na(temp_k)) {
return(NA)
} else if (temp_k < 0) {
warning('you passed in a negative Kelvin number')
# stop()
return(NA)
} else {
temp_c <- temp_k - 273.15
return(temp_c)
}
}
f_c <- function(temp_f) {
temp_k <- f_k(temp_f)
temp_c <- k_c(temp_k)
return(temp_c)
}
```
```{r}
for (pizza in f_values) {
print(pizza)
converted <- f_c(pizza)
print(converted)
}
```
```{r}
# 1:length(f_values)
# seq_along(f_values)
for (i in seq_along(f_values)) {
print(i)
val <- f_values[i]
print(val)
converted <- f_c(val)
print(converted)
}
```
### Pre allocating in a loop
```{r}
# prepopulate an empty vector
converted_values <- vector("double", length(f_values))
for (to_be_converted_position in seq_along(f_values)) {
converted <- f_c(to_be_converted_position)
converted_values[to_be_converted_position] <- converted
}
```
```{r}
converted_values
```
## purrr (map)
```{r}
library(purrr)
```
```{r}
map(f_values, f_c)
```
```{r}
map_dbl(f_values, f_c)
```
```{r}
mtcars
```
```{r}
map(mtcars, class)
```
```{r}
map_chr(mtcars, class)
```
```{r}
map_dbl(mtcars, mean)
```
```{r}
map(mtcars, summary)
```
## Apply (in base R)
apply family of functions
### lapply
```{r}
lapply(f_values, f_c)
```
### sapply
```{r}
sapply(f_values, f_c)
```
```{r}
v1 <- c(1, 2, 3, 4)
v2 <- c(100, 200, 300, 400)
```
### mapply
```{r}
my_mean <- function(x, y){
return((x + y) / 2)
}
```
```{r}
# sapply(v1, v2, my_mean)
```
```{r}
mapply(my_mean, v1, v2)
# this is the same as purrr::map2
```
### apply (2-dimensions)
```{r}
apply(mtcars, MARGIN = 1, mean)
```
```{r}
apply(mtcars, MARGIN = 2, mean)
```