# Loops are an essential building block for most computer algorithms.
# They allow for repetitive complex tasks to be broken into
# individual tasks that save coding and add flexibility.
# For simulation design, understanding how and when to use
# loops is an essential tool.
# Like most languages R has several forms of loops that
# do slightly different routines.
# The most common loop I use is the for loop
# This loop takes a vector and loops over that vector
# assigning values to the loop variable based on the position in the vector.
# This kind of loop is highly flexible.
for (i in 1:10) print(i)
# Will print a list from 1 to 10. This is because the
# argument 1:10 creates a count vector from 1 to 10.
1:10
# An increment of 1 may not be as useful in all circumstances:
for (i in seq(-.1,-1,-.1)) print(i)
# Though it is somewhat unimportant what the increment is since
# a count vector can always be transformed into any other linear combination.
# Thus also counts from -.1 to -1.
for (i in 1:10) print(i/(-10))
# For loops can loop though any arbitrary vector of arguments.
for (i in letters[1:10]) print(i)
# The vector that will be looped through does not need to be
# defined within the for loop.
pnames <- c("John", "Bob", "Sussy", "Pieter", "Xin-Xin", "Gonzales")
for (i in pnames) print(i)
# Loops can be used to loop through more than one command.
for (i in names) {
print(i)
print(i)
}
# Loops can be nested. Meaning that within one loop there
# can exist another loop which completes its internal loop
# for every repetition of the outer loop.
for (i in pnames) {
for (ii in 1:10) {
print (paste(i,ii))
}
}
# It is worth noting that single expression for loops as
# well as if statements can take their arguments on a
# single line following the statement.
for (i in pnames)
print(i)
# This can be useful if parts of the expression are lengthy.
# Nested loops can be nested in this way as well.
for (i in pnames)
for (ii in 1:10)
print (paste(i,ii))
# Another common loop form is the while loop which loops so
# long as the conditional statement is true:
i = 0
while (i<10) {
print(i+1)
i = i+1
}
# The while loop will continue to repeat the command so
# long as the condition is true.
# While loops are frequently used in instances when the
# number of loops required to complete a task is uncertain.
# Say you wanted to know how many times you would need to
# apply a function before a value converged or exceeded
# a particular value.
# For instance:
f <- function(x) (abs(sin(x))+.1)*(x+.1)*3
x = 0
while (x<100) {
x=f(x)
print(x)
}
# Notice that the entire block of code continues until the end
# denoted with a } at which point the command checks if the
# condition is still true.
# Obviously this is a very weird example which probably has
# no real world applications.
# However, there are many complex functions such as sorting
# algorithms for which it is impossible to know in advance the
# number and range of iterations necessary to accomplish a specific task.
# Another type of loop similar to the while loop is the repeat function.
# This function acts similar to the do while loop in other languages.
# The do while loop will not run the code even once if the
# condition has already been met.
# However the repeat command (or at least the structure that I
# have defined) will execute the code up until the point at
# which the break command is triggered.
x = 1000
repeat {
x=f(x)
print(x)
if (x>=100) break
}
# One repetition of the loop was run through before the "break"
# command tells R to stop repeating.
# The repeat command can also be broken mid way through
# a repetition of code.
x = 0
repeat {
x=f(x)
if (x>=100) break
print(x)
}
# Thus only prints 71.83 rather than the final value 110.77
# The repeat form of the code acts very similar to that of the
# while. The break command works for while loops as well as for loops.
for (i in 1:100) {
print(i)
if (i>50) break
}
# A similar command to break is the next command. Next tells the loop
# to advance to the next index value.
for (i in 1:100) {
# Uses the modular operator to check if i is odd
if (i %% 2 == 1) next
print(i)
}
# The above statement skips printing the odd values of i.
# Of course this could be more efficiently coded.
for (i in 2*(1:50)) print(i)
No comments:
Post a Comment