At the heart of a for
loop is the R object which determines the number of iterations your for
loop should make.
This counter variable (i
in this example) is declared inside the parenthesis immediately following the for
keyword. It is common to refer to the counter variable as the variable you "loop over".
Here, we loop over the variable i
, which is the vector [1, 2, 3]
. Inside the body of the for
loop, the variable i
takes on the values 1, 2, and 3, one at a time..
for (i in 1:3) {
print(i)
}
## [1] 1
## [1] 2
## [1] 3