According to the R base manual, among the control flow commands, the loop constructs are for, while and repeat, with the additional clauses *break and next. Control flow commands are the commands that enable a program to branch between alternatives, or to “take decisions”, so to speak.
You can always see these control flow commands by typing ?Control at the RStudio command line.
?Control
What are the components of a for loop? (There are many; this is the simplest)
Let’s follow an exmaple of a simple for loop that only squares the first 10 elements of a vector.
# Input --------------------------------
# rnorm(n_elements) is a function that creates random normal values; the argument is the number of elements
vector <- rnorm(30) # [-3,3]
# ---------------------------------------
# Ways of looping
# 1.) loop through variables in the vector itself
# 2.) indicies of the vector
# Initializing ---------------------
# Ways of initializing
# vector2 <- c(1,2,3,4)
# list2 <- list(1,2,3,4) # create an empty list: l <- list()
vector_sq <- 0 # var_sq <- c()
#-----------------------------------
for(i in 1:10) { # test expresion/condition
print(i)
print(vector[i])
vector_sq = vector[i]*vector[i] # Block of instruction (have to store calc made)
cat("Squared value is ", as.character(vector_sq))
}
[1] 1
[1] -0.02683
Squared value is 0.00071992207261667[1] 2
[1] 0.9421
Squared value is 0.887576779609611[1] 3
[1] -0.7922
Squared value is 0.627517432259795[1] 4
[1] -0.6085
Squared value is 0.370287545798415[1] 5
[1] -0.7717
Squared value is 0.595572695828718[1] 6
[1] 0.3559
Squared value is 0.12667846325068[1] 7
[1] -0.9242
Squared value is 0.854161872164456[1] 8
[1] -0.8654
Squared value is 0.748953923819284[1] 9
[1] -1.647
Squared value is 2.71235068256461[1] 10
[1] -1.723
Squared value is 2.9678587228965
Fig 1. For loop logics.
How to format a for loop:
Arrow. To store information into a variable, type an arrow that points towards the variable. An equal sign, = , does the same effect.
Parenthesis. What you are looping over is contained in parenthesis. This comes after the for loop andbefore hte curly braces.
Curly braces. The for block is contained within curly braces. These can be placed either immediately after the test condition or beneath it, preferably followed by an indentation. The curly braces enhance the readability of your code and allow to spot the loop block and potential errors within it easily.
Print(), cat(), or paste() functions Using these functions will help you see/check your end result. Place them throughout the block of instructions to see how the code progresses or after the loop. Print() can only print one variable/value of one class type (e.g. integer, character, numeric). Cat() stands for concatenate and you can put multiple values/variables (e.g. cat(var1, var2)) BUT they all have to be one class type. Paste() basically can do it all - you can have multiple values/variables and they can be of any class type (e.g. paste(“The squared value is”, squared_value))