Skip to content

Commit

Permalink
updated loop slides
Browse files Browse the repository at this point in the history
  • Loading branch information
RedondoMA committed Oct 29, 2024
1 parent 489f1d1 commit e17ab33
Showing 1 changed file with 87 additions and 46 deletions.
133 changes: 87 additions & 46 deletions slide_r_elements_4.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Where:

---
name: for_loop_1
# Repeating actions — for loop
# Repeating actions — for loop, an example

Example.

Expand All @@ -97,27 +97,11 @@ for (i in c(2,4,6,8,10)) {
}
```
The variable `i` <u> takes values </u> from the sequences.
---
name: for_loop_counter

# Repeating actions &mdash; for loop with a counter

To know the current iteration number on the loop, we can set an external counter:

```{r for.loop.cnt, echo=T}
cnt <- 1
for (i in c(2,4,6,8,10)) {
cat(paste('Iteration', cnt,
'Performing operation on no.', i), '\n')
cnt <- cnt + 1
}
```

---
name: for_loop_example

# Repeating actions &mdash; for loop, an example
# Repeating actions &mdash; for loop, another example

Say, we want to add 1 to every element of a vector:

Expand Down Expand Up @@ -172,10 +156,27 @@ proc.time() - ptm # for-loop
```


---
name: for_loop_counter

# Repeating actions &mdash; for loop with a counter

To know the current iteration number on the loop, we can set an external counter:

```{r for.loop.cnt, echo=T}
cnt <- 1
for (i in c(2,4,6,8,10)) {
cat(paste('Iteration', cnt,
'Performing operation on no.', i), '\n')
cnt <- cnt + 1
}
```

---
name: loops_avoid_growing

# Loops &mdash; avoid growing data
# Repeating actions &mdash; avoid growing data

Avoid changing dimensions of an object inside the loop:

Expand Down Expand Up @@ -274,7 +275,7 @@ proc.time() - ptm

name: if_clause

# Decisions, an if-clause
# Decisions, if-clause

Often, one has to take a different course of action depending on a flow of the algorithm.

Expand All @@ -293,7 +294,7 @@ A simple example:

```{r example if, echo=T}
temp<- -2
temp <- -2
if (temp < 0) {
print("It's freezing!")
Expand All @@ -304,9 +305,9 @@ if (temp < 0) {
---
name: if examples

# Decision, an if-clause
# Decisions, if-clause

Two examples of using `if` inside of a loop:
Two more examples of using `if` inside of a loop:

Let's display only the numbers that are greater than 5 in the sequence $[1, 10]$
```{r simple if, echo=T}
Expand Down Expand Up @@ -334,7 +335,7 @@ for (i in v) {
---
name:if_else

# Decisions &mdash; if-else
# Decisions, if-else

What if we want to perform an action when the first `if` condition is not met?

Expand Down Expand Up @@ -366,7 +367,7 @@ v <- 1:10
for (i in v) {
if (i %% 2 != 0) { # if clause
cat('o ')
} else { # another if-clause
} else { # else clause
cat('e ')
}
}
Expand All @@ -377,7 +378,7 @@ for (i in v) {
---
name: elif

# Decision taking &mdash; more alternatives
# Decisions, if-else-if for more alternatives

So far, so good, but we were only dealing with 2 alternatives.

Expand All @@ -388,11 +389,11 @@ We can use the **if-else-if** clause for this!
```{r if.elseif, echo=T}
v <- c(0:10)
for (i in v) {
if (i == 0) {
if (i == 0) { #if clause
cat('? ')
} else if (i %% 2 != 0) { # if clause
} else if (i %% 2 != 0) { # else-if clause
cat('o ')
} else { # another if-clause
} else { # else clause
cat('e ')
}
}
Expand Down Expand Up @@ -430,7 +431,7 @@ Often, it is really handy to re-use some code we have written or to pack togethe

This is the general syntax

```{r function syntax, eval=F, echo=T}
```
function_name <- function(arg1, arg2, ...) {
expr
return(something)
Expand All @@ -449,24 +450,11 @@ add.one <- function(arg1) {
add.one(1)
```

---
name:anatomy_of_a_fn

# Anatomy of a function

A function consists of: *formal arguments*, *function body* and *environment*:

```{r fns.formalsbodyenv, echo=T}
formals(add.one)
body(add.one)
environment(add.one)
environment(sd)
```

---
name: fns_defaults

# Functions &mdash; default values
# Functions &mdash; arguments with default values

Sometimes, it is good to use default values for some arguments:

Expand All @@ -475,8 +463,20 @@ add.a.num <- function(arg, num=1) {
arg <- arg + num
return(arg)
}
add.a.num(1, 5)
add.a.num(1) # skip the num argument
```

--

```{r functionresult, echo=T}
add.a.num(1, 5) # overwrite the num argument
add.a.num(1, num=5) # overwrite the num argument
```

--

```{r functionsresult2, echo=T, error=T}
add.a.num(num=1) # skip the first argument
```

Expand All @@ -490,11 +490,29 @@ args.demo <- function(x, y, arg3) {
print(paste('x =', x, 'y =', y, 'arg3 =', arg3))
}
args.demo(1,2,3)
args.demo(x=1, y=2, arg3=3)
```
--

```{r functions3b, echo=T}
args.demo(x=1, 2, 3)
```

--
```{r functions3c, echo=T}
args.demo(x=1, y=2, arg3=3)
```

--

```{r functions3d, echo=T}
args.demo(arg3=3, x=1, y=2)
```


---
name: variable_scope

Expand Down Expand Up @@ -631,6 +649,29 @@ Operators like `+`, `-` or `*` are using the so-called **infix** functions, wher
'a' %p% 'b'
```

---
name:anatomy_of_a_fn

# Anatomy of a function

A function consists of: *formal arguments*, *function body* and *environment*:

```{r fns.formalsbodyenv, echo=T}
formals(add.one)
```

--

```{r fns.formalsbodyenvb, echo=T}
body(add.one)
```

--
```{r fns.formalsbodyenvc, echo=T}
environment(add.one)
environment(sd)
```

---
name: base_fns

Expand Down

0 comments on commit e17ab33

Please sign in to comment.