Loop concepts - the for loop

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)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.255.139