# switch Statement in R (2 Examples) | How to Use the switch() Function

In this tutorial, I’ll illustrate how to apply the switch statement in R.

The content of the page is structured like this:

Let’s dig in:

## Definition & Basic R Syntax of switch Function

Definition: The switch R function runs a specific code block and returns its result.

Basic R Syntax: You can find the basic R programming syntax of the switch function below.

`switch(select_block, "code_block_1", "code_block_2", "code_block_3") # Basic R syntax of switch()`

In the remaining tutorial, I’ll show in two examples how to use the switch function in the R programming language.

## Example 1: Basic Application of switch() Function

In Example 1, I’ll show a very simple example to explain how to apply the switch function in R.

Let’s assume that we have three code blocks. Code block 1 returns the character string “first”, code block 2 returns the character string “second”, and code block 3 returns the character string “third”.

Then, we could select one of the code blocks using the switch function as shown below:

```switch(2, "first", "second", "third") # Apply switch function # "second"```

In this example, we told the switch function to return the second output, i.e. “second”.

Note that these code blocks could be as complex as you want. The switch function could therefore be used to choose between several different R programming codes.

## Example 2: Using switch within for-Loop

We can also use the switch function within a for-loop. Let’s assume that we have a vector that specifies several different code blocks that we want to run:

`my_input <- c(3, 1, 2, 4, 1) # Create input vector`

Then, we can use this vector to return the output of different code blocks:

```for(i in 1:length(my_input)) { # for-Loop with switch function my_output <- switch(my_input[i], "first", "second", "third") print(my_output) } #  "third" #  "first" #  "second" # NULL #  "first"```

Note that our input vector contained the value 4, but we have defined only three code blocks within the switch function. For that reason, the switch statement returned NULL at the index position of the value 4.

## Video, Further Resources & Summary

I have recently published a video on my YouTube channel, which shows the R syntax of this article. You can find the video below.

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party. If you accept this notice, your choice will be saved and the page will refresh.

In addition, you might want to have a look at the related posts of this homepage.

I recommend having a look at my tutorial about if else statements and the ifelse function, since ifelse is often a good alternative to switch statements.

A major difference between switch and ifelse is that switch is not vectorized! The switch statement only works for the first value in a vector, whereas the ifelse function evaluates all vector elements.

Furthermore, I have published several articles about other functions and statements in R already:

In summary: You learned in this article how to use the switch command in the R programming language. Don’t hesitate to let me know in the comments below, in case you have any additional questions.

Subscribe to the Statistics Globe Newsletter