R Statistics Blog

Data Science From R Programmers Point Of View

Conditional Statements & Loops

Conditional statements in R programing language enables the user to excute a statment based upon a particulare condition or criteria. On the other hand the loops ensure that the same task is excuted again and again.

Knowing and undertanding how these work are extremely critical for one success as a R Programer and in turn as a data analyst or a data science practitionar.

Things You Will Master

  1. if statement
  2. if else and nested if else statement
  3. switch statement
  4. for loop
  5. while loop
  6. How to break or skip a value in a loop

Using if statment in R Programming

If statement are used to check a condition and if the condition is met it executes a a block of code. It returns TRUE if the condition is met and returns FALSE if the condition is not met.

Quick look at the if statement syntax

# Syantx of if statement
if(condition){
  # block of code to be executed
}

Working example

Let us look at an example - Print the summary statistics of a variable only if the variable is numeric.

# Getting the iris data
data(iris)
# Executing the if statement
if(is.numeric(iris$Petal.Length)){
  print(summary(iris$Petal.Length))
}

Using if-else and nested if-else statement

In the above example you saw that the summary was printed as the variable type was numeric. However, if the class of variable would have been different no output would have been printed. In fact, nothing would have happened and leaving it like that is nota good idea especially if a user is involved. You may want to inform the user that the variable entered in not a numeric one and thus nothing is printed.

Quick look at the if-else statement syntax

# Syantx of if statement
if(condition){
  # block of code to be executed
} else {
  # block of code to be executed in case if returns FALSE
}

Working example

Let us take the above example and add a else statment which will print a statement notifying the user that input variable is not numeric.

# Executing the if statement
if(is.numeric(iris$Species)){
  print(summary(iris$Species))
} else {
  print("Tested variable is not a numeric variable.")
}

Quick look at the nested if-else statement syntax

The nested if else statement is used when you want to test two or more conditions.

# Syantx of nested if-else statement
if(first condition){
  # block of code to be executed
} else if(second condition) {
  # block of code to be executed if second condition is met
} else {
  # block of code to be executed if none of the conditions is met.
}

Working example

Let us take the above example and say we should print the summary statistics if the variable is numeric or integer and table if the variable is factor or character type. If none of the condition is met print we are sorry we do not accept this variable type.

# Reseting the data
data(iris)
# Getting the variable name
varName <- "Species"
# Executing the if statement
if(is.numeric(iris[, varName]) | is.integer(iris[, varName])){
  print(summary(iris$Species))
} else if(is.character(iris[, varName]) | is.factor(iris[, varName])) {
  print(table(iris[ , varName]))
} else {
  print("Variable type unknown")
}

You can have as many conditions in a nested if-else as you wish. However, remember that the if-else statement works from top to bottom. This may lead to unnessecarry computation at times based upon your analysis requirement.

Using switch statement in R Programming

If you are certain about all the output cases based upon the condition then it is recommended to use switch function over if-else statement.

Working example

Print the mean if the user passes “1” and print median if user passes “2”. We will use switch statement to achive this task.

# Refereshing the mtcars dataset
data(mtcars)

# printing mean
switch("1",
         "1" = mean(mtcars$mpg),
         "2" = median(mtcars$mpg))
}

Using for loop in R Programming

for loops are used to execute the same block of code over a range of values. This means as the range of values get overed the for loop ends.

Quick look at the for loop syntax

The sytax is pretty simple and straightforward. There are three components of the syntax. One, the vector in which we have the values. Two, the index itendtifier which will call one value at a time from the vector, and final part is the block of code which we will execute over and over.

# For loop syntax
for(i in vector){
  # Block of code
}

Working example

Print the class of all the variable using for loop.

varNamesList <- colnames(iris)
for(i in varNamesList){
  print(sprintf("The class of %s is %s.", i, class(iris[, i])))
}

Always Remember

To print the value while running a for loop we have to use print() function. In the above example I have to pass the sprintf() function output to print function in order to ensure that value gets printed on the console.

Using while loop in R Programming

A while loop is designed to execute when a condition is met and so it stops the execution of code as soon as the condition is not met.

Working Example

Print the a sequence of numbers starting from 1 till 10.

num <- 1
while(num <= 10){
  print(num)
  num = num + 1
}

How to break or skip a value in a loop

To skip a value or break out of loop in R we have two control statements called as next and break statement.

Working example

Print only those names from a character vector which have less than 5 alphabets and skip all the names which are spelled using more than five characters. To achieve this we will be using nchar() function and if statment inside a for loop.

# Defining a list of vector containg names
allNames <- c("John", "Michell", "Danny", "Silver", "Olive")

for (nam in allNames) {
  if (nchar(nam) > 5){
    next
  }
  print(nam)
}

Closing Note

In this chapter, we learned how to use conditional statements, loops and control statements in R. We used some simple and some little difficult examples to showcase the use of the same. But this is just the begining with time you will be able to create some very complicated programs using these simple things. In next chapter, we will look into apply family functions and we will also learn how to to define custom functions.
Last updated on 4 Jan 2019 / Published on 17 Oct 2017