R Switch Statement

The If Else Statement allows us to choose between TRUE or FALSE, and when there are more than two options, we use Nested If Else statement. Say, What if we have 12 alternatives to choose?, if we use Nested If Else in this situation, programming logic will be difficult to understand. In R Programming Switch statement and Else if statement can handle these types of problems effectively.

The working functionality of the switch case in R programming is almost the same as If Statement. As we said before, the Switch statement in R may have n number of options. So, the switch case compares the expression value with the values assigned in the position. If both the expression value and case value match, then statements present in that position will execute. Let us see the syntax of the R switch case for better understanding.

R Switch syntax

The basic syntax of the Switch in R Programming language is

switch (Expression, "Option 1", "Option 2", "Option 3", ....., "Option N")

Bit complicated syntactical approach of the Switch statement in r is:

switch (Expression,
        "Option 1" = Execute these statements when the expression result match Option 1,
        "Option 2" = Execute these statements when the expression result match Option 2,
        "Option 3" = When the expression result match Option 3, Execute these statements,
        ....
        ....
        "Option N" = When the expression result match Option N, Execute these statements,
        Default Statements
)
  • The expression value should be either integer or characters (We can write the expression as n/2 also, but the result should be an integer or convertible integers).
  • An R Switch statement allows us to add a default statement. If the Expression value or the Index_Position is not matching with any of the case statements, then the default statements will be executed.
  • If there is more than one match, the first matching statement will be returned.

We already discussed the Else If Statement in our previous R Programming article. So, let us explore the R Switch statement here. I suggest you to refer If Else Statement, Nested If Else statement, and If Statement articles.

R Switch case Flow Chart

The following screenshot will show you the flow chart behind this Switch Case

Flow Chart for R Switch Statement
  • If Case = Option 1, STATEMENT 1 is executed.
  • If Case = Option 2, STATEMENT 2 is executed.
  • Case = Option 3, STATEMENT 3 is executed.
  • If Option 1, Option 2, and Option 3 Fails, then DEFAULT STATEMENT is executed.

Switch Statement example

This program show you the basic functionality of the Switch Case in R programming language.

# R Switch Case Example

switch(3, 
       "Learn",
       "R Programming",
       "Tutorial",
       "Gateway"
)
R Switch Statement 3

Let us change the value and see

R Switch Statement 4

R Switch Case example

This program allows the user to enter any Arithmetic Operators to perform arithmetic operations using the Switch statement in R programming language.

# R Switch Statement Example

number1 <- 30
number2 <- 20
operator <- readline(prompt="Please enter any ARITHMETIC OPERATOR You wish!: ")

switch(operator,
       "+" = print(paste("Addition of two numbers is: ", number1 + number2)),
       "-" = print(paste("Subtraction of two numbers is: ", number1 - number2)),
       "*" = print(paste("Multiplication of two numbers is: ", number1 * number2)),
       "^" = print(paste("Exponent of two numbers is: ", number1 ^ number2)),
       "/" = print(paste("Division of two numbers is: ", number1 / number2)),
       "%/%" = print(paste("Integer Division of two numbers is: ", number1 %/% number2)),
       "%%" = print(paste("Division of two numbers is: ", number1 %% number2))
)

OUTPUT 1: Let us enter * operator and see

R Switch Statement 1

OUTPUT 2: Let us enter the wrong operator $ to check the default value

R Switch Statement 2

First, we declared two variables number1 and number2 and assigned some random values: 30 and 20

number1 <- 30
number2 <- 20

The following statement will allow the User to enter the single character (it should be any Arithmetic operator). And we are assigning the user entered character to a variable called an operator.

operator <- readline(prompt="Please enter any ARITHMETIC OPERATOR You wish!: ")

Next, we are using the R Switch statement with the operator as an option. If the user enters + as an operator, then the following statement printed.

print(paste("Addition of two numbers is: ", number1 + number2))

If the user enters – as an operator, then the following statement printed.

print(paste("Subtraction of two numbers is: ", number1 - number2))

When the user enters * as an operator, then the below statement printed.

print(paste("Multiplication of two numbers is: ", number1 * number2)

If the user entered operator (character) is not in any of the above then you can assign some default statement like:

switch(operator,
 "+" = print(paste("Addition of two numbers is: ", number1 + number2)),
 "-" = print(paste("Subtraction of two numbers is: ", number1 - number2)),
 "*" = print(paste("Multiplication of two numbers is: ", number1 * number2)),
 "^" = print(paste("Exponent of two numbers is: ", number1 ^ number2)),
 "/" = print(paste("Division of two numbers is: ", number1 / number2)),
 "%/%" = print(paste("Integer Division of two numbers is: ", number1 %/% number2)),
 "%%" = print(paste("Division of two numbers is: ", number1 %% number2)),
 print("default") # Default Statement
)