-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule_08_Flow_Control.Rmd
137 lines (99 loc) · 2.83 KB
/
Module_08_Flow_Control.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
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
---
title: 'Module 8: Flow Control'
author: "Brian P Steves"
date: "`r Sys.Date()`"
output: html_document
---
## If
If statements allow for conditional execution of code
```{r}
# random number between 1 and 10
x <- sample(10,1)
print(x)
guess <- 5
if (guess == x) {
print("Yes!")
}
if (guess > x) {
print("too high!")
}
if (guess < x) {
print("too low!")
}
```
## If Else
The else statement lets you specify a chunk of code to run when the if condition isn't met.
```{r}
coin <- c("Heads","Tails")
guess <- "Tails"
print(paste("You called", guess))
x <- sample(2,1)
print(paste("The flip is", coin[x]))
if(guess == coin[x]){
print("You win!")
} else {
print("You loose!")
}
```
## Ifelse
The Ifelse() function is a shortcut to an if else statement. It takes the following form.. ifelse(test, yes, no)
```{r}
coin <- c("Heads","Tails")
guess <- "Tails"
print(paste("You called", guess))
x <- sample(2,1) #random number 1 to 2
print(paste("The flip is", coin[x]))
ifelse(guess == coin[x], "You win!", "You loose!")
```
## For Loop
If you need to run a chunk of code a specific number of times you will want to use a `for` loop. Think.. "For every value in the sequence do the following"
```{r}
num_of_loops <- 10
for (i in 1:num_of_loops){
print(paste("loop number",i))
}
```
For loops are even more useful when you assign the number of loops based on your data. For example, you might want a loop for each row of data in your data frame. The nrow() function returns the number of rows in a data frame.
```{r}
library(readr)
dat<-read_csv("data/wk1sites.csv")
print(dat)
for (i in 1:nrow(dat)){
print(paste("Latitude:",dat$lat[i], ", Longitude:", dat$lon[i]))
}
```
## While
If you need to run a chunk of code a number of times, but you don't know in advance how many times you need to loop through the code you'll want to use a `while` loop. Think... "While the this condition is still true, do the following."
```{r}
x<-0
while (x < 5) {
x <- rnorm(1)*5
print(x)
}
```
## Repeat
A repeat loop is very similar to a while loop. A while loop won't even start if the conditional starts out as false, but a repeat loop will run once first and then check the conditional. You also have to specify when to stop using "break".
```{r}
repeat {
x<-rnorm(1)*5
print(x)
if (x > 5) break
}
```
## Break and Next
The break statement is used to stop a loop. The Next statement is used to skip the current loop. In the following example we will override the normal 10 loops of the for loop from our first example using next and break.
```{r}
num_of_loops <- 100
for (i in 1:num_of_loops){
if (i == 5) {
print("skip 5")
next
}
if (i > 7) {
print("stop after 7")
break
}
print(paste("loop number",i))
}
```
Notice that we skipped over loop number 5 using the `next` and stopped at loop 7 by using `break`.