Sunday, August 3, 2014

Introduction to R: Control Structure in R Part 1

In our previous discussion, we talked about other textual formats in R. In this blog we will discuss about the control structures which are commonly used when writing a R program, these structure ensures the control of flow in the R Language. There are several control functions that we will discuss here:
a. if and if, else - used to test a condition.
b.  for loops-  used to execute a loop for a fixed number of times.
c. while loops - used to execute a loop "while" a condition is proven true.
d. break - used to break an execution of a loop.
e. next - used to skip an iteration of a loop.
f. return - used to exit a loop.

Control Structure 1: if () {} else{} & if (){}, else if(){} else{}

The if(){} and if(){} else{}  function tests a logical condition. If the conditions are set to be true, the program will execute a command. If the conditions are set to be false, R will execute another set of commands.
The simplest form of the if(){} control structure is in the form of:

if(Condition){command
}else{
}

For two conditions the if(){}, else if {} structure takes in the form of:

if(Condition 1){command 1
} else if(Condition 2){command 2
}else{
}

If you have observed, the else{} syntax is usually written after each command.

For multiple conditions, then it is obvious that the control structure looks like:

if(condition 1){command 1}
if(condition 2){command 2}
if(condition 3){command 3}
if(condition 4){command 4}
.
.
.
.
.
if(condition N){command N}

the else{} is not necessary in the multiple conditions.

Control Structure 2: for(){} Loops

The for(){} loops is the most common type of looping operator in R. In this operator, there is a loop index which is commonly termed as i, however for multiple loops, the index can take the form of other letters ( k,l,m,n,o,p, etc.). The for(){} loops take an iterator variable and assign into it successive values from a sequence or vectors. the for(){} loops are commonly used over the elements of an object.

The simplest for(){} loops takes the form of:
for(){
command
}

Example 1:
Create a for(){} loop that will loop over numbers 1 to 20.



In this example, the loop takes the variable i and in each iteration of the loop it will print out a value of 1 to 20. After the last number is printed out, the command exits.

Example 2:
Create a  for(){} loop that will loop using the index i, which is sequenced from 1 to 5, the loop will print out the ith element which corresponds to the letter "a", "b", "c", "d", "e".



Example 3:
The seq_along() function takes over a vector as an input and creates an integer sequence which is equal to the length of the vector. Using the previous example, Example 2., we have a vector of length 5 if the seq_along() is used the command will create a vector sequence 1 to 5.



Example 4:
Using the previous example, Example 2., we will create a different index variable and call it "alphabet". This index variable will be taking values from an assigned vector, hence, the index variable does not need to be an integer (since it can take elements from any arbitrary vector). In this example, the for(){}loop is going through the vector c with values "a", "b", "c", "d", "e" and printing out the index variable which is equal to the letter.





















Nested for(){} Loops

A nested for(){} loops is similar to having a for(){} loops existing inside another  for(){} loop.

Example 5:
In this example we will create a matrix with 2 rows and 3 columns. The command will loop over the column with the outer loop that is the "i" index. This, "i" index will loop over the rows. Using the seq_len() function over this "i" index, we have created an integer sequence over the rows. Using another seq_len() function over the "j" index to take the number of columns and create an integer sequence over the column.























A word of caution, be careful when using nested for loops and never go beyond 2 to 3 levels as this will make the command difficult to understand by others.