You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: slide_r_elements_4.Rmd
+54-38Lines changed: 54 additions & 38 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Replication, Control Structures & Functions"
3
3
subtitle: "Elements of the R language"
4
-
author: "Marcin Kierczak and Nima Rafati"
4
+
author: "Marcin Kierczak, Nima Rafati, Miguel Redondo"
5
5
keywords: bioinformatics, course, scilifelab, nbis, R
6
6
output:
7
7
xaringan::moon_reader:
@@ -55,7 +55,7 @@ name: repeating_actions_1
55
55
56
56
# Repeating actions
57
57
58
-
Somtimes you want to repeat certain action several times.
58
+
Sometimes you want to repeat certain action several times.
59
59
60
60
There are few alternatives in R, for example:
61
61
-`for` loop
@@ -66,13 +66,19 @@ name: for_loop_0
66
66
67
67
# Repeating actions — for loop
68
68
69
-
One way to repeat an action is to use the **for-loop**
69
+
One way to repeat an action is to use the **for-loop**.
70
+
71
+
This is the general syntax:
70
72
71
73
```{r for.loop.general, eval=FALSE, echo=TRUE}
72
74
for (var in seq) {
73
75
expr
74
76
}
75
77
```
78
+
Where:
79
+
- var = variable that will take values from the sequence
80
+
- seq= sequence of values
81
+
- expr = expression to be executed
76
82
77
83
---
78
84
name: for_loop_1
@@ -82,17 +88,17 @@ Example.
82
88
83
89
```{r for.loop, echo=T}
84
90
for (i in 1:5) {
85
-
cat(paste('Performing operation no.', i), '\n')
91
+
cat(paste('Performing operation on no.', i), '\n')
86
92
}
87
93
```
88
94
89
95
--
90
96
91
-
A slight modification of the above example will skip odd indices.
97
+
A slight modification of the above example.
92
98
93
99
```{r for.loop2, echo=T}
94
100
for (i in c(2,4,6,8,10)) {
95
-
cat(paste('Performing operation no.', i), '\n')
101
+
cat(paste('Performing operation on no.', i), '\n')
96
102
}
97
103
```
98
104
@@ -106,9 +112,10 @@ Sometimes, we also want an external counter:
106
112
```{r for.loop.cnt, echo=T}
107
113
cnt <- 1
108
114
for (i in c(2,4,6,8,10)) {
109
-
cat(paste('Performing operation no.', cnt,
110
-
'on element', i), '\n')
115
+
cat(paste('Iteration', cnt,
116
+
'Performing operation on no.', i), '\n')
111
117
cnt <- cnt + 1
118
+
112
119
}
113
120
```
114
121
@@ -122,6 +129,11 @@ Say, we want to add 1 to every element of a vector:
122
129
```{r for.loop.ex1, echo=T}
123
130
vec <- c(1:5)
124
131
vec
132
+
133
+
```
134
+
135
+
```{r for.loop.ex2, echo=T}
136
+
125
137
for (i in vec) {
126
138
vec[i] <- vec[i] + 1
127
139
}
@@ -165,6 +177,37 @@ for (i in vec) {
165
177
proc.time() - ptm # for-loop
166
178
```
167
179
180
+
181
+
---
182
+
name: loops_avoid_growing
183
+
184
+
# Loops — avoid growing data
185
+
186
+
Avoid changing dimensions of an object inside the loop:
187
+
188
+
```{r avoid.growing, echo=T}
189
+
v <- c() # Initialize
190
+
for (i in 1:100) {
191
+
v <- c(v, i)
192
+
}
193
+
```
194
+
195
+
--
196
+
197
+
It is much better to do it like this:
198
+
199
+
```{r avoid.growing2, echo=T}
200
+
v <- rep(NA, 100) # Initialize with length
201
+
for (i in 1:100) {
202
+
v[i] <- i
203
+
}
204
+
```
205
+
206
+
--
207
+
208
+
Always try to know the size of the object you are going to create!
209
+
210
+
168
211
---
169
212
name: while_loop
170
213
@@ -174,14 +217,15 @@ There is also another type of loop in R, the **while loop** which is executed as
174
217
```{r loop.while, echo=T}
175
218
x <- 1
176
219
while (x < 5) {
177
-
cat(x, " ... ")
220
+
cat("x equals",x, "\n")
178
221
x <- x + 1
179
222
}
180
223
```
181
224
182
225
---
183
226
name: recursion
184
-
227
+
exclude: true
228
+
# Any questions so far?
185
229
<!-- # Recursion
186
230
187
231
When we explicitely repeat an action using a loop, we talk about **iteration**. We can also repeat actions by means of **recursion**, i.e. when a function calls itself. Let us implement a factorial $!$:
@@ -231,35 +275,7 @@ proc.time() - ptm
231
275
-->
232
276
233
277
---
234
-
name: loops_avoid_growing
235
-
236
-
# Loops — avoid growing data
237
-
238
-
Avoid changing dimensions of an object inside the loop:
239
-
240
-
```{r avoid.growing, echo=T}
241
-
v <- c() # Initialize
242
-
for (i in 1:100) {
243
-
v <- c(v, i)
244
-
}
245
-
```
246
278
247
-
--
248
-
249
-
It is much better to do it like this:
250
-
251
-
```{r avoid.growing2, echo=T}
252
-
v <- rep(NA, 100) # Initialize with length
253
-
for (i in 1:100) {
254
-
v[i] <- i
255
-
}
256
-
```
257
-
258
-
--
259
-
260
-
Always try to know the size of the object you are going to create!
0 commit comments