Control statements in R

Data Science for Everyone

Author

Bongani Ncube

Note

Hey there! , i’m a data and R enthusiast , i help write efficient R and help you to scale up you data science projects!

Decision making

in most cases you want to conditionally perform operations - or in other words, to perform different operations depending on the outcome of a test of desired conditions.

We can use if() {}, if() {} else {} and ifelse() to help us with this selection process.

if statement

In the example below, the expression is only executed if the condition that is being tested is TRUE.

in R

example

how about if you change the sign to <

Note
  • the code does not bring out any results

Note that when the condition is FALSE, nothing happens and the program carries on to the next step.

if else statement

The if statement only performs an operation if a condition is met. However, one may not only be interested in specifying the expression that must be done when the required condition is TRUE, but also the expression that must be performed when the condition is FALSE. To achieve this, we can use the if() {} else {} statement and ifelse() function.

using the last example let’s see what the following code will result in

Or ifelse

example

there is more

While the if() and if() else statements leave you with exactly two options, nested if() else statement allows you consider more alternatives.

else if in action

Exercise

  • change x to 15
  • change x to -2

Example 2

Use curly brackets { } so that R knows to expect more input. Try:

Here is a cheatsheet with logical operators that can help you completing certain tasks:

Command Meaning
== equal to
!= not equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to
x&y x AND y
x|y x OR y
isTRUE(x) test if x is true
  • Examine the if statement that prints out “Showing”facebook” information” if the medium variable equals “facebook”.

Exercise

  • Code an if statement that prints “You are popular!” to the console if the num_views variable exceeds 15.

if the number of views is greater than 15 then you are popular

Else statement

  • set medium=="twitter"

set num_views to 10

  • if number of views is greater than 15 then you are popular
Customize further: else if

The else if statement allows you to further customize your control structure. You can add as many else if statements as you like. Keep in mind that R ignores the remainder of the control structure once a condition has been found that is TRUE and the corresponding expressions have been executed. Here’s an overview of the syntax to freshen your memory:

Tip

if (condition1) { expr1 } else if (condition2) { expr2 } else if (condition3) { expr3 } else { expr4 }

Exercise

Add code to both control structures such that:

  • R prints out “Showing Facebook information” if medium is equal to “Facebook”. Remember that R is case sensitive!
  • “Your number of views is average” is printed if num_views is between 15 (inclusive) and 10 (exclusive). Feel free to change the variables medium and num_views to see how the control structure respond. In both cases, the existing code should be extended in the else if statement. No existing code should be modified.
Loops

While loop

  • The while loop is somewhat similar to the if statement because it executes the code inside if the condition is true. However, as opposed to the if statement, the while loop will continue to execute this code over and over again as long as the condition is true.
  • The syntax of a while loop is pretty similar to the if statement
Infinite while loop

some times if you run a while loop the wrong way the line result would be printed indefinitely, until we stop the session manually with Control C. Why? Because result does not get updated; this would mean that the condition is always true, and R keeps on re-executing the code in the while loop. You’ll have to hit the stop sign in your R console to stop this. What I truly want to say here: always make sure your while loop will end at some point!

break statement

The break statement. The break statement simply breaks out of the while loop: when R finds it, it abandons the currently active while loop. Suppose we want R to stop our while loop from before as soon as the value of ctr is divisible by 5. We can do this with a break statement.

Syntax

Warning

while (condition) { expr }

Exercise

Code a while loop with the following characteristics:

  • The condition of the while loop should check if speed is higher than 30.
  • Inside the body of the while loop, print out “Slow down!”.
  • Inside the body of the while loop, decrease the speed by 7 units and assign this new value to speed
  • again. This step is crucial; otherwise your while loop will never stop and your session will expire.
  • If your session expires when you run your code, check the body of your while loop carefully: it’s likely that you made a mistake.

Throw in more conditionals

In the previous exercise, you simulated the interaction between a driver and a driver’s assistant: When the speed was too high, “Slow down!” got printed out to the console, resulting in a decrease of your speed by 7 units.

There are several ways in which you could make your driver’s assistant more advanced. For example, the assistant could give you different messages based on your speed or provide you with a current speed at a given moment.

A while loop similar to the one you’ve coded in the previous exercise is already available for you to use. It prints out your current speed, but there’s no code that decreases the speed variable yet, which is pretty dangerous. Can you make the appropriate changes?

If the speed is greater than 48, have R print out “Slow down big time!”, and decrease the speed by 11. Otherwise, have R simply print out “Slow down!”, and decrease the speed by 6. If the session keeps timing out and throwing an error, you are probably stuck in an infinite loop! Check the body of your while loop and make sure you are assigning new values to speed.

play around with the numbers below ,carefull to understand the logic lest the code runs indefinitely

Exercise

Finish the while loop so that it:

  • prints out the triple of i, so 3 * i, at each run.
  • is abandoned with a break if the triple of i is divisible by 8, but still prints out this triple before breaking.
Hint

To check if the triple of i is divisible by 8, use the condition (i * 3) %% 8 == 0. The execution of %% has priority over * so you have to use parentheses to execute the multiplication first.

For loop

  • The for loop is somewhat different from the while loop. Have a look at this ‘recipe’.
Tip

for ( i in seq){ print(i) }

each var, a variable, in seq, a sequence, execute expressions. Makes sense? Let’s see how this actually works with an example. Suppose you have a vector, cities, containing the names of a number of cities. We can simply print the cities vector to the console. But suppose we want to have a different printout for every element in the vector. We can accomplish this using a for loop.

Note

consider the example above

Let’s start from the recipe and convert it to a functional for loop step by step.

  • Inside the parentheses, we write ‘city in cities’, meaning that we want to execute the code in the expression block for every city in the cities vector.

  • We’ll simply replace the expression by a simple print statement for starters.

How does R handle this code? - [x] At the start of the loop, R evaluates the seq element, being cities in our case. It realizes that it is a vector containing 6 elements. - [x] Next, R stores the first element of this sequence in the variable city, so city equals “New York” now.

  • The final result looks like this: for each city, a separate printout was done.

The for loop does not only work on vectors: it also works with lists for example. Suppose that the cities vector is a list instead of a vector: The exact same for loop as we’ve been using before can be used for lists, and the result is exactly the same. So there’s no need to worry about the difference between subsetting vectors and lists, because the for loop does this for us. I would encourage you to try the for loop with different data structures as well, such as matrices and data frames. I won’t go into detail.

Exercise

  • Loop over a vector

loop version 2

Loop over a list

Looping over a list is just as easy and convenient as looping over a vector. There are again two different approaches here:

more like a vector we say :

loop version 2

Notice that you need double square brackets - [[ ]] - to select the list elements in loop version 2.

example

Suppose you have a list of all sorts of information on New York City: its population size, the names of the boroughs, and whether it is the capital of the United States. We’ve already defined a list nyc containing this information (source: Wikipedia).

As in the previous exercise, loop over the nyc list in two different ways to print its elements:

Loop directly over the nyc list (loop version 1). Define a looping index and do subsetting using double brackets (loop version 2).

Loop version 2

Mix it up with control flow

Let’s return to the LinkedIn profile views data, stored in a vector linkedin. A little more in-depth interpretation of this data wouldn’t hurt, right? Time to throw in some conditionals! As with the while loop, you can use the if and else statements inside the for loop.

Add code to the for loop that loops over the elements of the linkedin vector:

  • If the vector element’s value exceeds 10, print out “You’re popular!”.
  • If the vector element’s value does not exceed 10, print out “Be more visible!”