if (condition) true_action
if (condition) true_action else false_action7 Control Flow
Control flow
In R, there are two primary tools of control flow: choices and loops
-
Choices allow you to run different code depending on the input
- e.g.
ifstatements andswitch
- e.g.
-
Loops allow you to repeatedly run code, typically with changing options
- e.g.
forandwhile
- e.g.
Choices
- The basic form of an
ifstatement in R as follows
true_actioncorresponds to the action for which theconditionistruefalse_actioncorresponds to the action for which theconditionisfalse
x <- sample(1:10, 1)
if (x >= 5) {
"x is greater than or equals 5"
} else {
"x is smaller than 5"
}#> [1] "x is smaller than 5"
When R evaluates the condition inside if statements, it is looking for a logical element, i.e., TRUE or FALSE
x <- 4 == 3
if (x) {
"Nothing will print as condition is FALSE"
}if (TRUE) {"Understand?"}#> [1] "Understand?"
- Write a function that takes the raw score obtained in an exam and returns the grade corresponding to the number.
grade <- function(x) {
if (x >= 90) y = "A"
else if (x >= 80) y = "B"
else if (x >= 60) y = "C"
else y = "D"
return(y)
}grade(77)#> [1] "C"
grade(95)#> [1] "A"
- The
dplyrpackage has a functioncase_when()to categorize a variable
grade_c(77)#> [1] "B"
grade_c(95)#> [1] "A"
if()function is used for single values.To sort a list of values into two categories, you can use the base R function
ifelse()or thedplyrpackage’sif_else()functionThe syntax of
if_else()
if_else(condition, true_action, false_action)Loops
-
forloops are used to iterate over items in a vector
for (item in vector) {
perform_action
}for (i in 1:3) {
print(i)
}#> [1] 1
#> [1] 2
#> [1] 3
#> [1] "this"
#> [1] "is"
#> [1] "a"
#> [1] "for"
#> [1] "loop"
The
nextstatement skips the current iteration of the loop and starts the loop from the next iterationThe
breakstatement terminate the execution of the loop.
for (i in 1:10) {
if (i < 3) next
if (i >= 5) break
print(i)
}#> [1] 3
#> [1] 4
- Sometimes you will find yourself needing to repeat an operation until a certain condition is met, rather than doing it for a specific number of times.
z <- 1
while (z < 4) {
z <- z + 1
print(z)
}#> [1] 2
#> [1] 3
#> [1] 4
Base functionals
A functional is a function that takes a function as an input and returns a vector as output.
-
apply()function takes a matrix or array as an input and return a lower dimensional summary
apply(X, MARGIN, FUN)X a matrix or an arrayMARGIN a vector giving the subscripts which the function will be applied over, e.g., for a matrix, 1 indicates a row and 2 indicates a columnFUN the function to be applied on each MARGINA data frame should not be used as an input in
apply()
mat <- matrix(1:15, nrow = 3)
mat#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1 4 7 10 13
#> [2,] 2 5 8 11 14
#> [3,] 3 6 9 12 15
- Row sum using loops
-
lapply()returns a list after applying a function on each element of an R object
lapply(X, FUN, ...)#> $x
#> [1] 1 2 3
#>
#> $y
#> [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4