The for
loop is the most popular looping construct in R. Using a for
loop, a similar task can be performed many times iteratively, let's look at a sample example where the for
loop concept is applied. In the following code, a series of numbers from 10 to 25 is created. The null vector v
is acting like a storage unit. If the condition mentioned in the following code is not met, the loop is never executed:
x<-100:200 y <- NULL # NULL vector as placeholder for(i in seq(along=x)) { if(x[i] < 150) { y <- c(y, x[i] - 50) } else { y <- c(y, x[i] + 50) } } print(y)
18.119.255.139