Easy R for M.F.Sc students – Part 6 (Loops)


Welcome to sixth chapter of Easy R tutorial. This chapter feature about use of loops in R and are essential if you have plans to develop programming skills. Looping helps you to write your own functions and handle advanced graphics with R. Before you start, let me remind you to use ‘ESC’ key on your keyboard if necessary. If your code is not correct, probably R will run for ever non-stop. Escape key will help you to stop R if you get screwed.

Loops are environment in which a particular job can be repeated as long as you wish. There are different types of loops depending upon the criteria you would  like to use.

1. Loop using….. for ( )

The for ( ) function is useful when you want to repeat a job for fixed number of times. An easy example is as follows:

for ( i in 1:10 )
 {
print ( i )
 }

The format for the function is

for ( number of times ) { do this job }

In the above example, we told R to print the value ‘i’ what ever is its value. To change the value of ‘i’ in each repeat, it was instructed within round brackets of ‘for ( )’ command. The value of ‘i’ was sequenced from 1 to 10 in the above example. So the result would look like this:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

For practice, I have two more example which you can try and see results.

 for(i in 1:1000)
{
  plot(runif(10,1,10),runif(10,1,5),col=i,cex=runif(1,1,5),pch=16,xlim=c(1,10),ylim=c(1,5))
 }

This above example is very fancy. Don’t worry about the details. We will come to those in later chapters. Just enjoy the fun 🙂

Try the following one too:

A<-1
B<-2

for ( a in 1:100 )
{
  A <- A+1
  B <- B*A*5
}

In this example, The equations A and B are repeated for 100 times. To see the result,  call A and B. I hope after this example, you must be clear about the concept of using a loop and the use of for ( ) function.

2. if else …..condition

“If else” function is not used for looping but mostly required while looping. At times you don’t want to repeat a job while some condition. This is known as a conditional looping. Let’s keep it simple. Take the first example of printing the value of ‘i’ 10 times. Our condition is not to print the value of ‘i’ if it is greater than 5.

The format of the function is

if (something) {do this} else {do this}

See the below example.

for ( i in 1:10 )
 {
   if ( i > 5 ) { } else { print ( i ) }
 }

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

In the above example we told R to do nothing when the value of ‘i’ is greater than 5.

3. Loop using ……….While ( )

If you do not know exactly how many repeats are required, you can tell R to stop repeating when a condition is satisfied. This is where While loop becomes useful. The general format for while command is

While (until this condition remain) {loop this job}

Let’s see the following example:

while ( i < 6 )
{
  print ( i )
  i <- i + 1
}

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

We told R to print the value of ‘i ‘ until i is less than 6. So we did the same example as before with a different function and style.

What if you do not know the number of repeats and do not know your condition until somehting happens later in the loop?

3. Loop using …….repeat {} break

A repeat…break is the solution for that. The general format for this is:

repeat {  this job

if (this happens) {break}   }

i <- 0

repeat
{
  print ( i )
  i <- i + 1
  if ( i > 6 ) { break }
}

[1] 0
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6

Here, the value of ‘i’ was printed until ‘i’ was greater than 6. The difference of this method from while looping is that, you do not have to mention your condition at the beginning of the loop.

One important thing to remember is that, if the condition never happens while running the code, R will never stop. So then you should understand something is wrong somewhere in your code and you might have ot fix it. To stop the running R, always remember to use Escape key.

We covered all the important looping functions and conditional statements which are used while programming with R.  From next chapter, we will be doing more interesting stuffs with population modelling. The chapters will focus on increasing your R skills than doing any statistics or proper modelling. Plotting R graphs demand more skills and knowledge. This will be one aspect I would be writing for many chapters in future. Thanks for reading my blog and leave a comment if you like this. Once again, thank you 🙂

Advertisements

About Deepak George Pazhayamadom

I'm a fish biologist and a mathematical modeller. I have a wide range of research interests, mostly centered on fisheries resource management.

Posted on February 12, 2012, in Bio-Statistics and Analysis. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: