These allow you to control the flow of execution of a script typically inside of a function. Common ones include:
We don't use these while working with R interactively but rather inside functions.
if (condition) {
# do something
} else {
# do something else
}
e.g.
x <- 1:15
if (sample(x, 1) <= 10) {
print("x is less than 10")
} else {
print("x is greater than 10")
}
Vectorization with ifelse
ifelse(x <= 10, "x less than 10", "x greater than 10")
Other valid ways of writing if/else
if (sample(x, 1) < 10) {
y <- 5
} else {
y <- 0
}
y <- if (sample(x, 1) < 10) {
5
} else {
0
}
A for
loop works on an iterable variable and assigns successive values till the end of a sequence.
for (i in 1:10) {
print(i)
}
x <- c("apples", "oranges", "bananas", "strawberries")
for (i in x) {
print(x[i])
}
for (i in 1:4) {
print(x[i])
}
for (i in seq(x)) {
print(x[i])
}
for (i in 1:4) print(x[i])
m <- matrix(1:10, 2)
for (i in seq(nrow(m))) {
for (j in seq(ncol(m))) {
print(m[i, j])
}
}
i <- 1
while (i < 10) {
print(i)
i <- i + 1
}
Be sure there is a way to exit out of a while
loop.
repeat {
# simulations; generate some value have an expectation if within some range,
# then exit the loop
if ((value - expectation) <= threshold) {
break
}
}
for (i in 1:20) {
if (i%%2 == 1) {
next
} else {
print(i)
}
}
This loop will only print even numbers and skip over odd numbers. Later we'll learn other functions that will help us avoid these types of slow control flows as much as possible (mostly the while
and for
loops).