Friday, November 27, 2020

R Switch Statement

 A switch statement is a selection control mechanism that allows the value of an expression to change the control flow of program execution via map and search.

The switch statement is used in place of long if statements which compare a variable with several integral values. It is a multi-way branch statement which provides an easy way to dispatch execution for different parts of code. This code is based on the value of the expression.

This statement allows a variable to be tested for equality against a list of values. A switch statement is a little bit complicated. To understand it, we have some key points which are as follows:

  • If expression type is a character string, the string is matched to the listed cases.
  • If there is more than one match, the first match element is used.
  • No default case is available.
  • If no case is matched, an unnamed case is used.

There are basically two ways in which one of the cases is selected:

1) Based on Index

If the cases are values like a character vector, and the expression is evaluated to a number than the expression's result is used as an index to select the case.

2) Based on Matching Value

When the cases have both case value and output value like ["case_1"="value1"], then the expression value is matched against case values. If there is a match with the case, the corresponding value is the output.

The basic syntax of If-else statement is as follows:

  1. switch(expression, case1, case2, case3....)  

Flow Chart

R Switch Statement

Example 1

  1. x <- switch(  
  2. 3,  
  3. "Shubham",  
  4. "Nishka",  
  5. "Gunjan",     
  6. "Sumit"  
  7. )  
  8. print(x)  

Output:

R Switch Statement

Example 2

  1. ax= 1  
  2. bx = 2  
  3. y = switch(  
  4.     ax+bx,  
  5.     "Hello, Shubham",  
  6.     "Hello Arpita",  
  7.     "Hello Vaishali",  
  8.     "Hello Nishka"  
  9. )  
  10. print (y)  

Output:

R Switch Statement

Example 3

  1. y = "18"  
  2. x = switch(  
  3.     y,  
  4.     "9"="Hello Arpita",  
  5.     "12"="Hello Vaishali",  
  6.     "18"="Hello Nishka",  
  7.     "21"="Hello Shubham"  
  8. )  
  9.    
  10. print (x)  

Output:

R Switch Statement

Example 4

  1. x= "2"  
  2. y="1"  
  3. a = switch(  
  4.     paste(x,y,sep=""),  
  5.     "9"="Hello Arpita",  
  6.     "12"="Hello Vaishali",  
  7.     "18"="Hello Nishka",  
  8.     "21"="Hello Shubham"  
  9. )  
  10.    
  11. print (a)  

Output:

R Switch Statement

Example 5

  1. y = "18"  
  2. a=10  
  3. b=2  
  4. x = switch(  
  5.     y,  
  6.     "9"=cat("Addition=",a+b),  
  7.     "12"=cat("Subtraction =",a-b),  
  8.     "18"=cat("Division= ",a/b),  
  9.     "21"=cat("multiplication =",a*b)  
  10. )  
  11.   
  12. print (x)  

Output:

R Switch Statement

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...