-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_R.qmd
610 lines (458 loc) · 37.3 KB
/
01_R.qmd
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# Introduction to R language {#sec-introduction}
R is a **dynamically typed interpreted** language. It is a highly capable enviornment for computing and graphics, for which R is often labeled a *glue* language. It has built-in functions for statistical computing.
Many great sources for R language with the focus on data science and statistical data processing are already written and free online. We will just name a few and limit the introduction at this site to bare minimum, which we use within this class.
**High quality R sources:**
- CRAN manuals like **Introduction to R** at <https://cran.r-project.org/doc/manuals/R-intro.pdf>
- [The R Book](https://www.cs.upc.edu/~robert/teaching/estadistica/TheRBook.pdf) is a very comprehensive source, although strongly oriented towards statistics
- freeCodeCamp's [R programming tutorial](https://www.youtube.com/watch?v=_V8eKsto3Ug&t=4052s&pp=ygUWciBwcm9ncmFtbWluZyBsYW5ndWFnZQ%3D%3D) is a very mild introduction to scripting language
- [Advanced R (2nd edition)](https://adv-r.hadley.nz/) from Hadley Wickham contains a lot of information about langugae fundamentals and extra information.
For the newcomer to R ecosystem we highly encourage to print and use the various official *cheat sheets*, created by the R community and **Posit Inc.**
[![](images/base-r-1.jpg){fig-align="center"}](images/base-r-cheat-sheet.pdf)
#### Some basic commands {.unnumbered}
```{r, eval=FALSE}
version # start by checking version of your R, should be at least 4.0 and above
getwd() # get working directory
ls() # list object in current environment
print() # evaluate to console
rm() # removes object from
View() # show internals of and object
...
```
Then there are commands that work with file structure such as
```{r, eval=FALSE}
list.files()
dir.create()
dir.exists()
file.exists()
...
```
and you can also invoke any shell/cmd command plus attributes with `system2()` interface.
## Getting help
It is entered into the console in the form `help(<function name>)` or `?<function name>`. If we would like to look directly into the code of the function, it is also possible, we just enter the name of the function in the console without brackets, or use the command `View(<function name>)`. In addition, R also has `help.search(<function name>)` under the shortcut `??,` which searches for full-text help across installed packages. Furthermore, it is still possible to search the R language mailing list using the function `RSiteSearch()`, which opens a new window of the predefined browser. In addition, thematically integrated help cards are very useful: `?Logical`, `?Constants`, `?Control`, `?Arithmetic`, `?Syntax`, `?Special` etc.
::: callout-tip
## Exercise
Try to find help to `DateTimeClasses`.
a) What do the `POSIXct` a `POSIXlt` represent?
b) What is the difference between them?
c) Find a function for calculating $5!$
:::
## R as scientific calculator
### Arithmetic operations
```{r, eval=TRUE, collapse=TRUE}
1 + 2 # addition
1 - 2 # subtraction
1 / 2 # division
1 * 2 # multiplication
1 %/% 2 # integer division
1 %% 2 # modulo oprator
```
### Special values
R is familiar with the concept of $\pm\infty$, hence `-Inf` and `Inf` values are at disposal. You will get them most probably as results from computation heading to $\frac{\pm1}{0}$ numerically. There are other special values like `NULL` (*null value*), `NA` (*not assigned*) and `NaN` (*not a number*). The concept of *not assigned* is one that is particularly important, since it has significant impact on the computed result ({@code-mean-rm}). `NA` is of default type `logical`. Otherwise it si possible to specify missing value in all other data type like `NA_real_` (matches double), `NA_integer_`, `NA_complex_` and `NA_character_`, these are all usable in pre-allocation of memory for data structures. Try to find the usage of functions `na.omit()`, `is.na()`, `complete.cases()`.
```{r}
x <- seq(1:10) #<1>
x[c(5,6)] <- NA #<2>
print(x)
mean(x) #<3>
mean(x, na.rm = TRUE) #<4>
```
1. General sequence of numbers
2. change some elements to not assigned
3. without removal
4. and with removal
### Set operations
For manipulating sets, there are a couple of essential functions `union()`, `intersect()`, `setdiff()` and operator `%in%`.
```{r, collapse=TRUE}
set_A <- c("a", "a", "b", "c", "D")
set_B <- c("a", "b", "d")
union(set_A, set_B)
intersect(set_A, set_B)
set_A %in% set_B
```
The operators fall in arithmetic, relation, assign categories and we also put set functions here.
+-------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------+
| Sign | Meaning |
+=============================================================+=================================================================================================================+
| `+` , `-` , `*` , `/` , `%%` , `%/%` , `**` nebo `^`, `%*%` | arithmetic operators (plus, minus, multiply, divide, modulo, integer division, power and matrix multiplication) |
+-------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------+
| `>` ,`>=` , `<` , `<=` , `==` , `!=` | relation operators (larger/smaller than, equal, not equal) |
+-------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------+
| `!` , `&` , `&&` , `|` , `||` | logical (negation, and, or) |
+-------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------+
| `~` | functional relationship |
+-------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------+
| `<-` , `=`, `<<-`, `->` | assign operator |
+-------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------+
| `$` | naming indexation in heterogenic structures |
+-------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------+
| `:` | rangea |
+-------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------+
| `isTRUE()` , `all()` , `any()` , `%in%` , `setdiff()` | set functions |
+-------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------+
### Mathematical functions
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Function | Meaning |
+===================================+================================================================================================================================================================================================================================================================================================================================================+
| `log(x)` | logarithm $x$ to the base $e$ |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `exp(x)` | $x(e^x)$ |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `log(x, n)` | logarithm $x$ base $n$ |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `log10(x)` | logarithm $x$ base $10$ |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `sqrt(x)` | square root from $x$ |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `factorial`(x) | $x!$ |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `choose(n, x)` | binomial coefficients\ |
| | $$ |
| | \binom{n}{k} = \frac{n!}{k!(n-k)!} |
| | $$ |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `ceiling(x)` | smallest integer large than $x$ |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `floor(x)` | largest integer before $x$ |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `trunc(x)` | closest number between $x$ a 0 |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `round(x, digits)` | round $x$ to $n$ decimals |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `signif(x, digits)` | round $x$ to $n$ significant numbers |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `cos(x)` , `sin(x)` , `tan(x)` | function ins rad |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `acos(x)` , `asin(x)` , `atan(x)` | inverse trigonometric functions |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `abs(x)` | absolute value |
+-----------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
::: callout-tip
## Exercise {.unnumbered}
Evaluate the following expressions:\
a) $1 + 3 \cdot (2 / 3)\:\mathrm{mod}\:3$\
b) $\dfrac{\sin(2.3)}{\cos(\pi)}$\
c) $\sum\limits_{i = 1}^{53}i$\
d) $\dfrac{-\infty}{0}$, $\dfrac{-\infty}{\infty}$, $\dfrac{0}{0}$\
e) $\left(\dfrac{2}{35}\right)^{0.5} \cdot 3 \cdot (2 / 3)$\
f) $20!$\
g) $\int_{0}^{3\pi} \sin(x) dx$
:::
#### Matrix operations {.unnumbered}
Let's say we have a set of linear equations
$$
\begin{matrix}
2x& - 3y& &= 3\\
& - 2y& + 4z &= 9\\
2x& + 13y& + 9z&= 10
\end{matrix}\\
$$ {#eq-sys-linear}
Solving {@eq-sys-linear} is a one-liner:
```{r}
A <- matrix(data = c(2, -3, 0, 0, -2, 4, 2, 13, 9), nrow = 3, byrow = TRUE)
B <- c(3, 9, 10)
solve(A, B)
```
## R as programming language
### Variables and name conventions
It is highly discouraged using spaces and diacritical marks in naming, like the Czech translation of the term *"variable"* - `proměnná`. Most programmers use either `camelNotation` or `snake_notation` for naming purposes. Obviously the R is *case-sensitive* so `camelNotation` and `CamelNotation` are two different things. Variables do not contain spaces, quotes, arithmetical, logical nor relational operators neither they contain special characters like `=`, `-`, \`\`. Objects cannot be named by key words.
##### Key words {.unnumbered}
`if`, `else`, `repeat`, `while`, `function`, `for`, `in`, `next`, `repeat`, `break`, `TRUE`, `FALSE`, `NULL`, `Inf`, `NaN`, `NA`, `NA_integer_`, `NA_real_`, `NA_complex_`, `NA_character_`
It is not recommended to inlude dot in the name, like `morava.prutoky`, and to match the names with commonly used functions. R is "case-sensitive" which means, that `X` does not equal `x`.
##### Exercise {.unnumbered}
Intuitively, we might be guided to load the data into the `data` variable. This is the wrong however, since `data()` is a function to access datasets that are part of the basic R installation. Try it out.
##### Some cases of possible but wrong naming {.unnumbered}
`aaa`, `Morávka průtok [m/s]`, `moje.proměnná`\
### Rules of quotation marks and parenthesses
Both represent the paired characters in R. Parenthesses are used in three versions: classical, square brackets and curly brackets (braces). All of them have specific non-overlaping usage.
- `()` **are always to be found right next to a function name** they delineate the space where function arguments are to be specified.\
- `[]` **are always use with the name of the object** (vector, array, list, ...) and signalize subselecting from the object.\
- `{}` **mark a block of code**, which should be executed at once.\
Quotation marks introduce text strings. Both "double" and 'single' quotes can be used completely at will, they just need to be closed with the same type. Back quotes are also common and are used, for example, to delimit a non-standard column name in a structure.
### Functions
You can define own functions using the `function()` construct. If you work in \*\*\*\*RStudio, just type `fun` and tabulate a snippet from the IDE help. The action produces {@code-function-snippet}.
```{r, eval=FALSE}
name <- function(variables) {
...
}
```
`name` is the name of the function we would like to create and `variables` are the arguments of that function. Space between the `{`and `}` is called a body of a function and contains all the computation which is invoked when the function is called.
Let's put Here an example of creating own function to calculate *weighted mean*
$$
\bar{x} = \dfrac{\sum\limits_{i=1}^{n} w_ix_i}{\sum\limits_{i=1}^{n}w_i},
$$ where $x_iw_i$ are the individual weighted measurements.
We define a simple function for that purpose and run an example.
```{r}
w_mean <- function(x, w = 1/length(x)) {
sum(x*w)/sum(w)
}
w_mean(1:10)
```
Here is a different example:
```{r}
x <- rnorm(100)
nejblizsi_hodnota <- function(x, value) { # <1>
x[which(abs(x - value) == min(abs(x - value)))] # <1>
} # <1>
cat("Hodnota nejblíže 0 z vektoru x je:" , nejblizsi_hodnota(x = x, value = 0))
```
1. Example of function, which seeks the neares number from a vector `x` to a certain referential `value`.
We can test if we get the same result as the *primitive* function from R using `all.equal()` statement.
```{r}
all.equal(w_mean(x = 1:5, w = c(0.25, 0.25, 1, 2, 3)),
weighted.mean(x = 1:5, w = c(0.25, 0.25, 1, 2, 3)))
```
Any argument without default value in the function definition has to be provided on function call. You can frequently see functions with the possibility to specify `...` a so-called *three dot construct* or *ellipsis*. The **ellipsis** allows for adding any number of arguments to a function call, after all the named ones.
### Data types
The basic types are **logical**, **integer**, **numeric**, **complex**, **character** and **raw**. There are some additional types which we will encounter like **Date**. Since R is dynamically typed, it is not necessary for the user to declare variables before using them. Also the type changes without notice based on the stored values, where the chain goes from the least complex to the most. The summary is in the following table
```{r, collapse=TRUE}
TRUE # logical, also T as short version
1L # integer
1.2 # numeric
1+3i # complex
"A" # character, also 'A'
```
They represent the individual elements of data structures. R dynamically typed and does not require declarations before usage.
+---------------+--------------+------------------------+------------------------+------------------------+-------------+
| | logical | integer | numeric | complex | character |
+===============+==============+========================+========================+========================+=============+
| **logical** | `logical` | `integer` | `numeric` | `complex` | `character` |
+---------------+--------------+------------------------+------------------------+------------------------+-------------+
| **integer** | `logical` | `integer` | `numeric` | `complex` | `character` |
+---------------+--------------+------------------------+------------------------+------------------------+-------------+
| **numeric** | `logical` | `numeric` | `numeric` | `complex` | `character` |
+---------------+--------------+------------------------+------------------------+------------------------+-------------+
| **complex** | `logical` | `integer` + warning | `numeric` + warning | `complex` | `character` |
+---------------+--------------+------------------------+------------------------+------------------------+-------------+
| **character** | `NA_logical` | `NA_integer` + warning | `NA_numeric` + warning | `NA_complex` + warning | `character` |
+---------------+--------------+------------------------+------------------------+------------------------+-------------+
: Basic types and coercions.
Two types of functions are connected to data types: `is.___` a `as.___`. Is is either questioning or coertion of data type. Try also `class()`, `mode()`.
```{r}
is.character("ABC")
as.integer(11 + 1i)
```
::: callout-tip
## Exercise
a) Create in any way a vector `x` of 10 different numerical values, where $x\in\mathbb{R}$.
b) Write an expression to select numbers between -5 and 5 from this vector.
c) Convert to integer type and discuss the result.
d) Add 3 positions "A", "B" and "C" to the vector, has the vector changed?
:::
### Data structures
#### Vectors {.unnumbered}
Atomic vectors are single-type linear structures. They can contain elements of any type, from **logical**, **integer**, **numeric**, **complex**, **character**. A vector is a basic building structure in the R language, there is nothing like a scalar quantity here. The concept of vector is understood here in the mathematical sense as a vector of values representing a point in $n$-dimensional space.
$$
\mathbf{\mathrm{u}} =
\begin{pmatrix}
1\\
1.5\\
-14\\
7.223\\
\end{pmatrix}, \qquad
\mathbf{\mathrm{v}} =
\begin{pmatrix}
\mathrm{TRUE}\\
\mathrm{FALSE}\\
\mathrm{TRUE}\\
\mathrm{TRUE}\\
\end{pmatrix}, \qquad
\mathbf{\mathrm{u^T}} =
\begin{pmatrix}
1 & 1.5 & -14 & 7.233\\
\end{pmatrix}
$$
Many functions lead to creation of a vector, among the most used are `vector(mode = "numeric", length = 10)`, function `c()`, or using subset operators `[` or `[[`.
An important rule is tied to vectors - **value recycling**.
```{r}
v <- c(1.4, 2.0, 6.1, 2.7)
u <- c(2.0, 1.3)
u + v # <1>
u * v # <2>
u * 2.3 # <3>
```
1. Adding two vectors while length of second is the multiple of the first
2. Multiplying two vectors while length of second is the multiple of the first
3. Multipling with single numeric value
##### Working with vectors {.unnumbered}
```{r}
x <- 1:10 #<1>
x <- seq(10:1) #<1>
x <- vector(mode = "numeric", length = 10) #<1>
x <- replicate(n = 10, expr = eval(2)) #<1>
x <- sample(x = 10, size = 10, replace = TRUE) #<1>
x <- rep(x = 15, times = 2) #<1>
x <- rnorm(n = 10, mean = 2, sd = 20) #<1>
t(x) * x #<2>
names(x) <- LETTERS[1:length(x)] #<3>
x[x > 0] #<4>
x[1:3] #<5>
```
1. Vector creation $\boldsymbol{\mathrm{x}}$ by different approaches. Sequences, repeats, repetitions and sampling
2. Transposition of vector.
3. Naming elements of a vector.
4. Selection of elements from a vector based on a condition.
5. Selection of elements from a vector based on an index.
```{r}
#| label: test-code-annotation
#| echo: fenced
V <- vector(mode = "numeric", length = 0) # empty numeric vector creation
V[1] <- "A"
```
#### Matrices and arrays {.unnumbered}
If the object has more than one *dimension*, it is treated as an array. A special type of **array** is a **matrix**. Both object types have accompanying functions like `colSums()`, `rowMeans()`.
| Function | Meaning |
|--------------------|--------------------------------------|
| `nrow()`, `ncol()` | number of rows, columns in matrix |
| `dim()` | dtto |
| `det()` | matrix determinant |
| `eigen()` | eigenvalues, eigenvectors |
| `colnames()` | column names in matrix |
| `rowSums()` | row sums in matrix |
| `colMeans()` | column means of matrix |
| `M[m, ]` | Selection of $m$-th row of matrix |
| `M[ ,n]` | Selection of $n$-th column of matrix |
: List of `matrix` bounded functions
```{r}
x <- c(1:10)
dim(x) <- c(2, 5) #<1>
x
```
1. Conversion to $2\times 2$ dimension
```{r, collapse=TRUE}
M <- matrix(data = 0, nrow = 5, ncol = 2) # empty matrix creation
M[1, 1] <- 1 # add single value at origin
M[, 1] <- 1.5 # store 1.5 to the whole first column
M[c(1,3), 1:2] <- rnorm(2) # store random numbers to first two rows
colMeans(M)
rowSums(M)
```
It is possible to have matrices containing any data type, e.g.
$$
M = \left(\begin{matrix}
\mathrm{A} & \mathrm{B}\\
\mathrm{C} & \mathrm{D}
\end{matrix}\right),\qquad
N = \left(\begin{matrix}
1+i & 5-3i\\
10+2i & i
\end{matrix}\right)
$$
#### Data frames {.unnumbered}
`data.frame` structure is the workhorse of elementary data processing. It is a possibly heterogenic table-like structure, allowing storage of multiple data types (even other structures) in different columns. A *column* in any data frame is called a **variable** and *row* represents a single **observation**. If the data suffice this single condition, we say they are in **tidy** format. Processing *tidy* data is a big topic withing the R community and curious reader is encouraged to follow the development in **tidyverse** package ecosystem.
```{r}
thaya <- data.frame(date = NA,
runoff = NA,
precipitation = NA) # new empty data.frame with variables 'date', 'runoff', 'precipitation' and 'temperature'
#thaya$runoff <- rnorm(100, 1, 2)
```
#### Lists {.unnumbered}
**List** is the most general basic data structure. It is possible to store vectors, matrices, data frames and also other lists within a list. List structure does not pose any limitations on the internal objects lengths.
```{r, eval=TRUE}
l <- list() # empty list creation
l["A"] <- 1
print(l)
l$A <- 2
print(l)
```
#### Other objects {.unnumbered}
Although R is intended as functional programming language, more than one object oriented paradigm is implemented in the language. As new R users we encounter first OOP system in functions like `summary` and `plot`, which represent so called **S3 generic functions**. We will further work with **S4** system when processing geospatial data using proxy libraries like `sf` and `terra`. The OOP is very complex and will not be further discussed within this text. For further study we recommend OOP sections in **Advanced R** by Hadley Wickham.
```{r}
```
### Control flow
Condition and cycles govern the run of the general flow of calculation, they are the building blocks of algorithms.
#### Conditions
A condition in code creates branching of computation. Placing a condition creates at least two options from which only one is to be satisfied. The condition is created either by `if()`/`ifelse()` or `switch()` construct. We can again call for a snippet from RStudio help resulting in
```{r, eval=FALSE}
if (condition) {
...
}
switch (object,
case = action
)
ifelse(test, TRUE, FALSE)
```
##### `if()` {.unnumbered}
```{r}
A <- 1
if(A >= 1) {
cat("A larger than or equal 1.")
}
```
```{r}
A <- 5
if(A >= 2) { #<1>
cat("A is larger than or equal 2.") #<1>
} else if(A > 2) { #<1>
cat("A is larger than 2.") #<1>
} #<1>
```
1. The chain of conditions will close **at the first evaluation which happens to be TRUE**.
##### `ifelse()` {.unnumbered}
Vectorized condition, in general looks like
```{r}
x <- -5:5
cat("Element x + 3 is more than 0: ", ifelse(x - 3 > 0, yes = "Yes", no = "No"))
```
#### `switch()` {.unnumbered}
```{r}
variant <- "B"
2 * (switch(
variant,
"A" = 2, # <1>
"B" = 3)) # <2>
```
1. "A" variant did not happen,
2. instead the "B" variant is truthful, so the expression is evaluated as $2\cdot 3 = 6$
::: callout-tip
## Exercise
Create a following grading scheme:
| Grade | Result |
|-------|--------------|
| A | 90 % - 100 % |
| B | 75 % - 89 % |
| C | 60 % - 74 % |
| D | \< 60 % |
:::
### Loops
Loops (cycles) provide use with the ability to execute single statement of a block of code in `{}` multiple times. There are three key words for loop construction. They differ in use cases.
#### `for` cycle {.unnumbered}
Probably the most common loop is used when you **know the number of iterations prior to calling**. The iteration is therefore explicitly finite.
```{r, eval=FALSE}
for (variable in vector) {
...
}
```
An example
```{r}
for(i in 1:4) cat(i, ". iteration", "\n", sep = "")
```
#### `while` cycle {.unnumbered}
**while** is used in when it is impossible to state how many times something should be repeated. The case is rather in the form *while some condition is or is not met, repeat what is inside the body*. It is also used in intentionally infinite loop e.g. operating systems.
```{r}
i <- 1
while(i < 5) {
cat("Iteration ", i, "\n", sep = "")
i <- i + 1
}
```
#### `repeat` cycle {.unnumbered}
In the cases when we need the repetition at least once, we will evaluate the code inside *until* a condition is met.
```{r}
i <- 1
repeat { #<1>
cat("Iteration", i, "\n") #<1>
i <- i + 1 #<1>
if(i >= 5) break #<2>
} #<1>
```
1. Execute in loop,
2. if a condition is met, `break` stops the cycle.
#### `break` and `next` {.unnumbered}
There are two statements which controls the iteration flow. Anytime `break` is called, the rest of the body is skipped and the loop ends. Anytime `next` is called, the rest of the body is skipped and next iteration is started.
::: callout-tip
## Exercise
a) Create a cycle, which for the numbers $x={1, 2, 3, 4, 5}$ writes out $x^3$.
b) Calculates the cumulative sum for these.s
c) Calculates the factorial number for the number `x`.
d) Withe the help of `readline()` function (requests a number from the user),
prints the number. If the given number is negative, the loop ends.
:::