In this article we will show how to use switch statement in R. It comes in handy when working with lengthy if else statements.


Theory

In R programming we often have tasks where an input has many possible values and the output varies every time. Our natural approach is to solve is using if statements, but what should you do when you need to build a 10 level if else statement?

For example, you face the following task: there is a big race with over 300 runners. Top 10 places all get some prize. You are asked to develop a chunk of code that takes a runner’s place as an input (1, … , 10) and prints out what prize they should get.

Here is a list of prizes for each finisher:

$$ \begin{matrix} \begin{array}{c|c} \text{Place} & \text{Prize} \\ \hline 1 & \text{Gold Medal} \\ 2 & \text{Silver Medal} \\ 3 & \text{Bronze Medal} \\ 4 & \text{Jacket} \\ 5 & \text{Shoes} \\ 6 & \text{Bag} \\ 7 & \text{T-Shirt} \\ 8 & \text{Hat} \\ 9 & \text{Towel} \\ 10 & \text{Bracelet} \\ \end{array} \end{matrix} $$

Switch statement in R saves us many lines of code and works as a substitute to a multi level nested if else statement.



Application

Below are the steps we are going to take to make sure we do learn how to do use switch statement in R:

  1. Basic overview of switch() function in R
  2. How to write a switch statement in R


Part 1: Basic overview of switch() function in R

Here is a code framework of the switch statement in R:

switch(variable, list)

Here, variable is the input that will trigger the switch function and list contains comma-separated values that should be returned by the switch statement. The function examines the input (variable) and returns a specified value from the list.

switch() function can accept variable in both integer and string format. We will look into both cases.



Part 2: How to write a switch statement in R

Now let’s consider two possible examples for different types of input and then build our switch statement in R:

  1. Input variable is an integer (place = 1, …, 10)
  2. Input variable is a string (place = “First”, …, “Tenth”)

The variable should always be defined before the switch statement, unless switch statement is a part of a function which then is applied to this variable.

How to write switch statement in R with an integer expression

First, we need create the variable for place and assign some integer between 1 to 10 to it.


place <- 4

Now, let's create our switch statement. Since we have the variable in the integer form, we need to simply add the outputs in a comma-separated format. The switch statement will print the value assigned to the variable automatically.


switch(place,"Gold","Silver","Bronze", "Jacket", "Shoes", "Bag", "T-Shirt", "Hat", "Towel", "Bracelet")

Output:

[1] "Jacket"

The output is correct. Let's give it another try and find out what will the 6th place finisher get as a prize:


place <- 6

switch(place,"Gold","Silver","Bronze", "Jacket", "Shoes", "Bag", "T-Shirt", "Hat", "Towel", "Bracelet")

Output:

[1] "Bag"

Exactly what we expected!

Now let's move onto a more complicated example.


How to write switch statement in R with an integer expression

The setting is the same as in the section above with only one difference: now the input variable comes as a string. For 1st place finisher instead of "1" it will be "First", instead of 2 it will be "Second", and so on.

Since it's not an integer anymore, switch() doesn't treat it as an index for the value in the list. We will need to specific values should be returned from a list for each string expression input.

First, we need create the variable for place and assign some string expression to it, for example: "Third".


place <- "Third"

Now, we need to modify our switch statement to account for the new type of input.


switch(place,
       First = "Gold",
       Second = "Silver",
       Third = "Bronze",
       Fourth = "Jacket",
       Fifth = "Shoes",
       Sixth = "Bag",
       Seventh = "T-Shirt",
       Eighth = "Hat",
       Ninth = "Towel",
       Tenth = "Bracelet"
)

Output:

[1] "Bronze"

It produced the result we expected. What we did is is for every value in the list we created a string lookup which should correspond to one of the 10 entries that can be made.

Can you imagine creating an if else statement for this task? It would be 3 times more lines of code! So this function is very useful in these cases.

Let's run it one more time with a different place to make sure everything is correct.


place <- "Sixth"

switch(place,
       First = "Gold",
       Second = "Silver",
       Third = "Bronze",
       Fourth = "Jacket",
       Fifth = "Shoes",
       Sixth = "Bag",
       Seventh = "T-Shirt",
       Eighth = "Hat",
       Ninth = "Towel",
       Tenth = "Bracelet"
)

Output:

[1] "Bag"

The output is correct. At this point we have completed the task and it can be solved in two possible ways which we discussed above.



Creating correct switch statements in R is very useful when you know that the task will involve multiple if statements on a single variable and isn't complicated in terms of expected output. You can also find more related articles in our Data Manipulation section.