6 Control statements

In the previous chapter we learnt about many of the useful pre-built functions in R. In this chapter we will learn how to create customized functions suited to our needs.

Though these are core concepts of a programming language, yet a reading to this chapter is advised for better understanding and better application while using r for data analytics.

6.1 Control flow/Loops

if else

The basic form(s) of if else statement in R, are-

if (test) do_this_if_true
if (test) do_this_if_true else else_do_this

So, if test is true, do_this_if_true will be performed and optionally if test is not true else_do_this will be performed. See this example-

x <- 50
if(x < 10){
  'Smaller than 10'
} else {
  '10 or more'
}
## [1] "10 or more"

Note that the if/if else are evaluated for a single TRUE or FALSE i.e. this control flow is not vectorised as we have in the case of ifelse() function which was vectorised.

for loop

The for loops in r are used to iterate over given items. So, the basic structure of these loops are -

for(item in vector) perform_some_action

# OR

for(item in vector) {
  perform_some_action
}

Thus, for each item in vector, perform_some_action is called once; updating the value of item each time. This can be understood by the following simple example-

for(i in 1:3){
  print(i)
}
## [1] 1
## [1] 2
## [1] 3

Conventionally i has been used in above example to iterate over given vector 1:3, however any other symbol may also be used.

for(item in 1:3){
  print(item)
}
## [1] 1
## [1] 2
## [1] 3

If we use the name of any existing variable as item to iterate over the given object, for loop assigns the item to the current environment, overwriting any existing variable with the same name. See this example -

x <- 500
for(x in 1:3){
  # do nothing
}
x
## [1] 3
A Diagrammatic representation of For Loop

Figure 6.1: A Diagrammatic representation of For Loop

The idea can also used to iterate over any object any number of times as we want. See these two examples.

Example-1

my_names <- c('Andrew', 'Bob', 'Charles', 'Dylan', 'Edward')
# If we want first 4 elements
for(i in 1:4){
  print(my_names[i])
}
## [1] "Andrew"
## [1] "Bob"
## [1] "Charles"
## [1] "Dylan"

Example-2

# if we want all elements
for(i in seq_along(my_names)){
  print(my_names[i])
}
## [1] "Andrew"
## [1] "Bob"
## [1] "Charles"
## [1] "Dylan"
## [1] "Edward"

There are 2 ways to terminate any for loop early-

  • next which exits the current iteration only
  • break which breaks the entire loop.

See these examples.

Example-1

for(i in 1:5){
  if (i == 4){
    next
  }
  print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 5

Example-2

for(i in 1:5){
  if (i == 4){
    break
  }
  print(i)
}
## [1] 1
## [1] 2
## [1] 3

while loop

We have seen that for loop is used to iterate over a set of known values or at least known number of times. If however, we want to perform some iterative action unknown number of times, we may use while loop which iterates till a given condition is TRUE. Another option is to have repeat loop which can be used to iterate any number of times till it encounters a break.

The basic syntax of while loop is-

while (condition) action

See these examples-

Example-1

i <- 1
while(i <=4){
  print(LETTERS[i])
  i <- i+1
}
## [1] "A"
## [1] "B"
## [1] "C"
## [1] "D"

We may check the value of i here after executing the loop

i
## [1] 5

Example-2:

i <- 4
while(i >=0){
  print(LETTERS[i])
  i <- i-1
}
## [1] "D"
## [1] "C"
## [1] "B"
## [1] "A"
## character(0)

Note: We have to make sure that the statements inside the brackets modify the while condition so that sooner or later the given condition is no longer TRUE otherwise the loop will never end and will go on forever.

Author's illustration of While Loop

Figure 6.2: Author’s illustration of While Loop

Looping in R can be inefficient and time consuming when you’re processing the rows or columns of large data-sets. Even one of greatest feature in R is its parallel processing of vector objects. Thus, whenever possible, it’s better to use R’s built-in numerical and character functions in conjunction with the apply family of functions.(We will discuss these in detail in the chapter related to functional programming)