Previous: For-Loops Up: For-Loops Next: Example 2: Creating dummy

Example 1: Creating a vector with a logical statement

x <- array()             # Initializes an empty data structure.  
for (i in 1:10) {        # Loops through every value from 1 to 10, replacing
  if (is.integer(i/2)) { #  the even values in `x' with i+5.
    x[i] <- i + 5 
  }      
}                        # Enclose multiple commands in {}.
You may use for() inside or outside of functions.



Gary King 2011-11-29