Chapter 12 for
loops
A for()
loop works in the following way:
for(i in sequence) {
expression }
The letter i
can be replaced with any variable name, sequence
can be elements or the position of these elements, and expression
can be anything. Try the examples below:
for (a in c("Hello", "R", "Programmers")) {
print(a)
}
for (z in 1:4) {
<- rnorm(n = 1, mean = 5 * z, sd = 2)
a print(a)
}
## [1] "Hello"
## [1] "R"
## [1] "Programmers"
## [1] 6.269854
## [1] 12.56227
## [1] 13.18625
## [1] 19.96041