A switch statement enables a variable to be tested for equality against a list of values. The value of the switch() function is called a case, and the variable being switched on is checked for each case.
The nested if…else statement in R is exceptionally beneficial when you need to execute a particular code block when the condition is satisfied. If you want to select values based only on the condition, use the switch() function in R.
The switch in r is a built-in function that tests the expression against the elements of a list. If the value evaluated from the expression matches an item from the list, a corresponding value is returned. The switch() function takes an expression and list as arguments and returns the result based on the case.
switch (expression, list)
Here, the expression parameter is evaluated, and based on this value, the corresponding item in the list is returned.
If the value evaluated from the expression matches more than one element, the switch() function returns the first matched element.
Keypoints of the switch in R
- It is an expression type with a character string always matched to the listed cases.
- It is an expression that is not a character string, and then this expression is coerced to an integer.
- For multiple matches, the first match element will be used.
- No default argument case is available there in the R switch case.
- An unnamed case can be used if there is no matched case.
data <- switch( 2, "Millie", "Sadie", "Winona", "Robin" ) print(data)
The switch() function takes mainly two arguments: expression and list.
In our example, the expression is 2, and the list is the names of actresses. That means it returns the 2nd actress who is Sadie. The switch() function returns the corresponding item to the numeric value evaluated.
If the cases are values like a character vector, and the expression is evaluated to a number, then the expression’s result is used as an index to select the case.
If the numeric value is out of range (greater than the number of elements in the list or smaller than 1), NULL is returned.
data <- switch( 5, "Millie", "Sadie", "Winona", "Robin" ) print(data)
In this example, there is no 5th element in the list, so it returns NULL.
switch() function in r with a string expression
You can use the string as an expression in the switch() function in R Language.
show <- switch( "name", "name" = "Paranormal", "region" = "Arabic", "platform" = "Netflix" ) print(show)
Here, “name” is a string that matches the first element of the list. Hence, we are getting “Paranormal” as an output.
If you pass the platform as an argument, it will return Netflix.
We will get an error as a result only when there is more than one case statement with a wrong spelling, or that expression is not in the list, or the corresponding value of the case statement is missing.
R switch() with a mix values
We can pass integers and strings as different cases to the switch() function. In this case, it will first perform the operations between the variables, and whatever output we get, it will execute that case based on the output number.
x <- 0 y <- 1 mix <- switch(x + y, "Buy Reliance", "Buy TCS", "Buy HCL", "Buy Infosys", ) mix
 "Buy Reliance"
In this example, we pass two integer variables – whose values are 0 and 1, and inside the switch statement, the first argument is the sum of those variables’ values. So the sum of 0 and 1 is 1, and that’s why it returns the first case, “Buy Reliance“.
The switch() function in R follows the mapping approach and searches over a list of values. If more than one match for the specific value, the switch() function will return the first match, found of the value matched with the expression.
Switch statements are more efficient if we compare them with the If-Else statement.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.