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 the Nested If Else statement. Say, What if we have 12 alternatives to choose from?, if we use Nested If Else in this situation, programming logic will be difficult to understand. In R Programming Switch statement and Else if 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 the code present in that position will execute. Let us see the syntax of the switch case for better understanding.

Switch case in R Syntax

The basic syntax of the Switch case 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 when the expression result match Option 1,
        "Option 2" = Execute when the expression result match Option 2,
        "Option 3" = When the expression result match Option 3, Execute these,
        ....
        ....
        "Option N" = When the expression result match Option N, Execute these lines,
        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 integer).
  • An R Switch statement allows us to add a default statement. The default statements will be executed if the Expression value or the Index_Position does not match any of the cases.
  • The first matching statement will be returned if there is more than one match.

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

Switch case Flow Chart

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

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

R Switch Statement Example

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

# Example

switch(3, 
       "Learn",
       "R Programming",
       "Tutorial",
       "Gateway"
)
Simple Example 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.

# 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 the * operator and see

R Switch Statement 1

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

Result of the Wrong Value 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 a 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 code is printed.

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

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

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

The message below is printed when the user enters * as an operator.

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
)