-
Notifications
You must be signed in to change notification settings - Fork 0
/
Control-flow.Rmd
183 lines (132 loc) · 4.17 KB
/
Control-flow.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Control flow
```{r Control-flow-1, include = FALSE}
source("common.R")
```
## Choices (Exercises 5.2.4)
**Q1.** What type of vector does each of the following calls to `ifelse()` return?
```{r Control-flow-2, eval = FALSE}
ifelse(TRUE, 1, "no")
ifelse(FALSE, 1, "no")
ifelse(NA, 1, "no")
```
Read the documentation and write down the rules in your own words.
**A1.** Here are the rules about what a call to `ifelse()` might return:
- It is type unstable, i.e. the type of return will depend on the type of which condition is true (`yes` or `no`, i.e.):
```{r Control-flow-3}
ifelse(TRUE, 1, "no") # `numeric` returned
ifelse(FALSE, 1, "no") # `character` returned
```
- It works only for cases where `test` argument evaluates to a `logical` type:
```{r Control-flow-4}
ifelse(NA_real_, 1, "no")
ifelse(NaN, 1, "no")
```
- If `test` is argument is of logical type, but `NA`, it will return `NA`:
```{r Control-flow-5}
ifelse(NA, 1, "no")
```
- If the `test` argument doesn't resolve to `logical` type, it will try to coerce the output to a `logical` type:
```{r Control-flow-6}
# will work
ifelse("TRUE", 1, "no")
ifelse("false", 1, "no")
# won't work
ifelse("tRuE", 1, "no")
ifelse(NaN, 1, "no")
```
This is also clarified in the docs for this function:
> A vector of the same length and attributes (including dimensions and `"class"`) as `test` and data values from the values of `yes` or `no`. The mode of the answer will be coerced from logical to accommodate first any values taken from yes and then any values taken from `no`.
**Q2.** Why does the following code work?
```{r Control-flow-7}
x <- 1:10
if (length(x)) "not empty" else "empty"
x <- numeric()
if (length(x)) "not empty" else "empty"
```
**A2.** The code works because the conditional expressions in `if()` - even though of `numeric` type - can be successfully coerced to a `logical` type.
```{r Control-flow-8}
as.logical(length(1:10))
as.logical(length(numeric()))
```
## Loops (Exercises 5.3.3)
**Q1.** Why does this code succeed without errors or warnings?
```{r Control-flow-9, results = FALSE}
x <- numeric()
out <- vector("list", length(x))
for (i in 1:length(x)) {
out[i] <- x[i]^2
}
out
```
**A1.** This works because `1:length(x)` works in both positive and negative directions.
```{r Control-flow-10}
1:2
1:0
1:-3
```
In this case, since `x` is of length `0`, `i` will go from `1` to `0`.
Additionally, since out-of-bound (OOB) value for atomic vectors is `NA`, all related operations with OOB values will also produce `NA`.
```{r Control-flow-11}
x <- numeric()
out <- vector("list", length(x))
for (i in 1:length(x)) {
print(paste("i:", i, ", x[i]:", x[i], ", out[i]:", out[i]))
out[i] <- x[i]^2
}
out
```
A way to do avoid this unintended behavior is to use `seq_along()` instead:
```{r Control-flow-12}
x <- numeric()
out <- vector("list", length(x))
for (i in seq_along(x)) {
out[i] <- x[i]^2
}
out
```
**Q2.** When the following code is evaluated, what can you say about the vector being iterated?
```{r Control-flow-13}
xs <- c(1, 2, 3)
for (x in xs) {
xs <- c(xs, x * 2)
}
xs
```
**A2.** The iterator variable `x` initially takes all values of the vector `xs`. We can check this by printing `x` for each iteration:
```{r Control-flow-14}
xs <- c(1, 2, 3)
for (x in xs) {
cat("x:", x, "\n")
xs <- c(xs, x * 2)
cat("xs:", paste(xs), "\n")
}
```
It is worth noting that `x` is not updated *after* each iteration; otherwise, it will take increasingly bigger values of `xs`, and the loop will never end executing.
**Q3.** What does the following code tell you about when the index is updated?
```{r Control-flow-15}
for (i in 1:3) {
i <- i * 2
print(i)
}
```
**A3.** In a `for()` loop the index is updated in the **beginning** of each iteration. Otherwise, we will encounter an infinite loop.
```{r Control-flow-16}
for (i in 1:3) {
cat("before: ", i, "\n")
i <- i * 2
cat("after: ", i, "\n")
}
```
Also, worth contrasting the behavior of `for()` loop with that of `while()` loop:
```{r Control-flow-17}
i <- 1
while (i < 4) {
cat("before: ", i, "\n")
i <- i * 2
cat("after: ", i, "\n")
}
```
## Session information
```{r Control-flow-18}
sessioninfo::session_info(include_base = TRUE)
```