Skip to content

Commit

Permalink
Add base for loop and adjust switch example
Browse files Browse the repository at this point in the history
  • Loading branch information
nimarafati committed Oct 20, 2023
1 parent 3b8db30 commit 6ca7318
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions slide_r_elements_4.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,26 @@ which denotes multiplication of elements $1 ... n$.
It is important to learn how to translate these (and similar) formulas into the R language.

---
name: for_loop
name: for_loop_0

# Repeating actions — for loop

One way to repeat an action is to use the **for-loop**

```{r for.loop.general, echo=T, eval = F}
for (var in seq) {
expr
}
```

---
name: for_loop_1

# Repeating actions — for loop

Example.


```{r for.loop, echo=T}
for (i in 1:5) {
cat(paste('Performing operation no.', i), '\n')
Expand Down Expand Up @@ -252,7 +266,7 @@ Always try to know the size of the object you are going to create!
---
name: if_clause

# To R or not to Python? — taking decisions, an if-clause
# An if-clause

Often, one has to take a different course of action depending on a flow of the algorithm. You have already seen the **if-else** block. Let's print only odd numbers $[1, 10]$:

Expand Down Expand Up @@ -351,8 +365,8 @@ If-else clauses operate on logical values. What if we want to take decisions bas
```{r switch, echo=T}
switch.demo <- function(x) {
switch(class(x),
logical = ,
numeric = cat('Numeric or logical.'),
logical = cat('logical'),
numeric = cat('Numeric'),
factor = cat('Factor.'),
cat('Undefined')
)
Expand Down Expand Up @@ -519,7 +533,7 @@ h <- function(a = 1, b = d) {
h()
```

> The above won't be possible in, e.g. C where values of both arguments have to be known before calling a function **eager evaluation**.
> The above won't be possible in, e.g. where values of both arguments have to be known before calling a function which is known as **eager evaluation**.
---
name: everything_is_a_fn
Expand Down

0 comments on commit 6ca7318

Please sign in to comment.