R Switch Statement

In this tutorial you will learn about the R Switch Statement and its application with practical example.

R Switch Statement

In R, switch statement evaluates equality of an variable/expression value against multiple case values in order to identify the block of code to be executed.

Syntax:-

Here, expression values is tested against multiple case values (case1, case2, case3…., caseN)

The following rules apply to a switch statement –

  • If the expression value is not a character string it is coerced to integer.
  • In switch you are allowed to have any number of case statements.
  • If there is more than one match, the first matching element is returned.
  • There is no default value/case is available.
  • If no match found and there is an unnamed element, its value is returned.

Example:-

Output:-

r_switch_statement

r_switch_statement

In this tutorial we have learn about the R Switch Statement and its application with practical example. I hope you will like this tutorial.