-
Notifications
You must be signed in to change notification settings - Fork 1
/
lessons_loops_conditionals.Rmd
110 lines (88 loc) · 3.01 KB
/
lessons_loops_conditionals.Rmd
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
---
title: "Loops and Conditionals"
---
## Learning Objectives
- Write and use `for` loops to apply the same code to multiple objects or elements
- Use `if`-`else` statements to conditionally execute code
## Required Packages and Datasets
Make sure you can load all these packages and dataset before starting the module
```{r, message = FALSE}
library(dplyr)
library(uwpols501)
data(iver)
```
## For Loops
We use `for` loops to iterate through multiple elements of a list, variable, etc., and do **something** with those elements.
A `for` loop has the following skeleton:
```{r, eval = FALSE}
for (each_element in here) { # Try to use meaningful names for the
# do something # "each_element" object
print(each_element)
}
```
A short/silly example:
```{r}
list_numbers <- sample(x = 1:100, size = 10, replace = FALSE) # sample() function
for (number in list_numbers) {
print(paste0("2 times ", number, " is ", number * 2))
}
```
A real example:
Imagine that we want to calculate the $log$ of the variables `povred` and `enp` of the `iver` dataset. We can create a list with these two variable names and then, for each of them, find them in `iver`, calculate the $log$, and add the result as a new variable to the dataset.
```{r}
numeric_vars <- c("povred", "enp")
for (variable in numeric_vars) {
y <- iver[,variable]
log_y <- log(y)
name_new_var <- paste0(variable, "_log")
iver[,name_new_var] <- log_y
}
```
## Conditional Execution: if and else statements
Sometimes we only want to execute certain code if the data fulfills some conditions. To do that we use `if` and `else` statements.
How `if` and `else` statements look like:
```{r eval=FALSE}
if (this) {
# do that
} else if (that) {
# do something else
} else {
#
}
```
**This** and **that** in the previous chunk of code are boolean tests: code that returns TRUE/FALSE when the computer executes it. Some examples of boolean tests:
```{r}
10 == 2
10 %in% c(2, 5, 13, 20, 10)
10 == 2 & 10 %in% c(2, 5, 13, 20, 10)
10 == 2 | 10 %in% c(2, 5, 13, 20, 10)
```
A short/silly example:
```{r}
some_numbers <- c(1, 4, 6, 10,12, 16, 45, 88, 102)
for (number in some_numbers) {
if (number < 10) {
print(paste0(number, " is smaller than 10"))
} else if (number < 50) {
print(paste0(number, " is smaller than 50 but greater or equal to 10"))
} else {
print(paste0(number, " is greater than 50"))
}
}
```
Real example:
We previously calculated the $log$ and added it to the dataset for the numeric variables in `iver`. To do that we first created a list with the numeric variables. This time we won’t create that list. Instead, we’ll use conditional execution to indicate that we only want to take the $log$ of numeric variables.
```{r}
data(iver) # load the data set again
iver <- as.data.frame(iver)
for (variable in names(iver)) {
y <- iver[,variable]
if (is.numeric(y)) {
log_y <- log(y)
name_new_var <- paste0(variable, "_log")
iver[,name_new_var] <- log_y
} else{
print(paste0(variable, " is not a numeric variable."))
}
}
```