Writing while loops in R

Writing while loops can be sort of dangerous for beginners because if not done correctly you can write a loop that will never stop running (an infinite loop). To try to avoid this, I want to give a brief review of conditional statements in R. First off, a conditional statement can either evaluate as true or false, there are no other options. For example, 5 == 5 will always be true (which would make an infinite loop). Let’s go through some possible conditional statements and see if they evaluate as true or false. Notice how I use “==” instead of a single “=”, 1 equal sign is for assignment whereas 2 is for conditional statements.

x <- 5

x == 5 # x equals 5 evaluates as true

x < 6 # x is less than 6 evaluates as true

x >= 5 # x is greater than or equal to 5 evaluates as true

x != 6 # x does not equal 6 evaluates as true

is.numeric(x) == TRUE # x is numeric evaluates as true (although we are really saying that how is.numeric() evaluates equals true)

is.character(x) == TRUE # x is a character evaluates as false

Everything probably seems straightforward here, as we can use standard mathematical notation to define logical statements with a few exceptions. One conditional statement I often use in if statements is something like x %% 5 == 0. The “%%” is the operator of finding the remainder of division, so every time x is a multiple of 5 the statement will evaluate as true. Now back to while statements, a while statement will run as long as the conditional statement it is given evaluates as true. Naturally, the basic setup then is to make a conditional statement that will change over time and eventually evaluate as false when it no longer needs to keep running. We are going to start with an essentially pointless loop that counts to 5, for the sake of demonstration. I am going to start with a variable “x”, and add 1 to it each time the loop runs. I added some extra code that will print out exactly what is happening in each step, the print line is not really necessary.

x = 0

while(x < 5){ # The conditional statement goes in the parentheses

print(paste("We have the number",x,"and add 1 to get",x+1))

x = x + 1

}

Now let us consider a more practical example that involves running averages. We want to see how large of a sample we need to collect to reach a sample mean that is close enough to the population mean within a given tolerance. This is a perfect use of a while statement, as it will keep running until the sample mean is sufficiently close to the population mean. We start by setting a seed so my random numbers are generated repeatedly, then we generate “x” with rnorm() making the population mean 5 and standard deviation 1. Next, we specify the tolerance we want the sample mean to achieve, start a counter, and initialize the difference we are calculating. Note that we need the difference to start off higher than the tolerance, or the while loop will never start to run. Now we just need the while loop to repeatedly take the mean of increasingly large subsets of the x string until the difference is less than the tolerance. You will see that it took 137 steps for the specified tolerance. If you change the tolerance, you may also need to change how many numbers the “x” vector contains, as you can run out. Also, we are not answering the real question you may want to know, which is “How large of a sample do we need on average to reach that tolerance?”. You would need to run this same loop many times to determine that (without set.seed()), which is not really the point of this tutorial.

set.seed(123) # For repeatability

x = rnorm(1000,5,1) # Creates a vector of 1000 random numbers

tol = 0.001 # The tolerance we are going to

counter = 0 # The counter is initialized

diff = 1 # The difference is initialized

while(diff > tol){

counter = counter + 1

mean = mean(x[1:counter]) # Each time the loop runs 1 more number is included in the mean

diff = abs(mean-5) # The absolute value of the difference

}

print(paste("It took", counter, "steps to reach the value", mean," with a tolerance of", tol)) # Reads out the result

I hope you found this tutorial helpful! I would generally recommend using for loops over while loops if you can because of how easy it is to make infinite loops accidentally. However, while loops can be indispensable when you need your code to keep running until some unknown cutoff is reached, such as in the example above. You can also use while loops in other optimization problems, to ensure that the optimization is run for enough iterations to reach the desired minima or maxima.

Previous
Previous

Scrambling the letters of a message with R

Next
Next

Using For Loops in R