Skip to content

Commit 9ddcd68

Browse files
committed
modified elements 4 slides
1 parent 65a7a57 commit 9ddcd68

File tree

1 file changed

+54
-38
lines changed

1 file changed

+54
-38
lines changed

slide_r_elements_4.Rmd

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Replication, Control Structures & Functions"
33
subtitle: "Elements of the R language"
4-
author: "Marcin Kierczak and Nima Rafati"
4+
author: "Marcin Kierczak, Nima Rafati, Miguel Redondo"
55
keywords: bioinformatics, course, scilifelab, nbis, R
66
output:
77
xaringan::moon_reader:
@@ -55,7 +55,7 @@ name: repeating_actions_1
5555

5656
# Repeating actions
5757

58-
Somtimes you want to repeat certain action several times.
58+
Sometimes you want to repeat certain action several times.
5959

6060
There are few alternatives in R, for example:
6161
- `for` loop
@@ -66,13 +66,19 @@ name: for_loop_0
6666

6767
# Repeating actions — for loop
6868

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:
7072

7173
```{r for.loop.general, eval=FALSE, echo=TRUE}
7274
for (var in seq) {
7375
expr
7476
}
7577
```
78+
Where:
79+
- var = variable that will take values from the sequence
80+
- seq= sequence of values
81+
- expr = expression to be executed
7682

7783
---
7884
name: for_loop_1
@@ -82,17 +88,17 @@ Example.
8288

8389
```{r for.loop, echo=T}
8490
for (i in 1:5) {
85-
cat(paste('Performing operation no.', i), '\n')
91+
cat(paste('Performing operation on no.', i), '\n')
8692
}
8793
```
8894

8995
--
9096

91-
A slight modification of the above example will skip odd indices.
97+
A slight modification of the above example.
9298

9399
```{r for.loop2, echo=T}
94100
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')
96102
}
97103
```
98104

@@ -106,9 +112,10 @@ Sometimes, we also want an external counter:
106112
```{r for.loop.cnt, echo=T}
107113
cnt <- 1
108114
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')
111117
cnt <- cnt + 1
118+
112119
}
113120
```
114121

@@ -122,6 +129,11 @@ Say, we want to add 1 to every element of a vector:
122129
```{r for.loop.ex1, echo=T}
123130
vec <- c(1:5)
124131
vec
132+
133+
```
134+
135+
```{r for.loop.ex2, echo=T}
136+
125137
for (i in vec) {
126138
vec[i] <- vec[i] + 1
127139
}
@@ -165,6 +177,37 @@ for (i in vec) {
165177
proc.time() - ptm # for-loop
166178
```
167179

180+
181+
---
182+
name: loops_avoid_growing
183+
184+
# Loops &mdash; 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+
168211
---
169212
name: while_loop
170213

@@ -174,14 +217,15 @@ There is also another type of loop in R, the **while loop** which is executed as
174217
```{r loop.while, echo=T}
175218
x <- 1
176219
while (x < 5) {
177-
cat(x, " ... ")
220+
cat("x equals",x, "\n")
178221
x <- x + 1
179222
}
180223
```
181224

182225
---
183226
name: recursion
184-
227+
exclude: true
228+
# Any questions so far?
185229
<!-- # Recursion
186230
187231
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
231275
-->
232276

233277
---
234-
name: loops_avoid_growing
235-
236-
# Loops &mdash; 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-
```
246278

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!
261-
262-
---
263279
name: if_clause
264280

265281
# Decisions, an if-clause

0 commit comments

Comments
 (0)