Basic loops in R
If we want to perform an action repeatedly in R, we can utilize the loop functionality.
How to do it…
The following lines of code multiply each element of x
and y
and store them as a vector z
:
How it works…
In the preceding code, a calculation is executed 10 times. R performs any calculation specified within {}
. We are instructing R to multiply each element of x
(using the x[i]
notation) by each element in y
and store the result in z
.