Sunday, November 11, 2018

How to use Switch Case Statement in R Programming

Switch Case Statement in R Programming
The functionality of the switch case in R programming is almost same as If Else Statement. Switch statement may have n number of options and 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.
Syntax :
switch (Expression,
        "Option 1" = Execute these statement1,
        "Option 2" = Execute these statement2,
        "Option 3" = Execute these statement3,
        ....
        ....
        "Option N" =  Execute these statementN,
        Default Statement
)


here :
The expression value should be either integer or characters (We can write the expression as n/2 also but the result should be integer or convertible integers).
R Switch statement allows us to add 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.


Examples :
>switch(3,
        "Learn",
        "R Programming",
        "ExcelKingdom",
        "Blog"
)

[1] "ExcelKingdom"


The following program allows the user to enter any R Arithmetic Operators to perform arithmetic operations using Switch Case in R programming language.

> number1 <- 30
> number2 <- 20
> operator <- readline(prompt="Please enter any ARITHMETIC OPERATOR You wish!: ")
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)),

+        print ("default statement")
+ )

[1] "Multiplication of two numbers is: 600"

In the following, the default statement will be executed, as the value is not matches any of the cases.
> operator <- readline(prompt="Please enter any ARITHMETIC OPERATOR You wish!: ")
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)),
+        print ("default statement")
+ )

[1] "default statement"

Notes:
If the value of expression is not a character string it is coerced to integer.
You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
If the value of the integer is between 1 and nargs()−1 (The max number of arguments)then the corresponding element of case condition is evaluated and the result returned.
If expression evaluates to a character string then that string is matched (exactly) to the names of the elements.
If there is more than one match, the first matching element is returned.
No Default argument is available.
In the case of no match, if there is a unnamed element of ... its value is returned. (If there is more than one such argument an error is returned.)


Thanks, TAMATAM

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.