Site icon R-bloggers

A switch-case construct for the R language

[This article was first published on Topics in R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In contrast to many other programming languages, R has no native switch-case statement. Often, however, it is useful to have an efficient way of testing multiple, similar conditions while avoiding nested ifelse constructs which make the code less clear and readable. The switchcase package offers a switch-case construct for R. It is encapsulated in the package’s main function, switchCase(). switchCase() allows to define multiple ‘case’ branches (alternatives) that consist of a condition and a code block that is executed if the condition is fulfilled. Also, it can be specified if the switch-case construct shall be left after one alternative code block has been executed, or if the other (following) conditions shall be tested, as well. This ‘break’ behavior can be defined on the level of the whole switch-case construct or on the level of each individual alternative, with the alternative’s ‘break’ behavior setting overruling the construct-level option. Also, an alternative branch of a switch-case construct can return a value (which is then the return value of the switchCase() function). To understand how the switchCase() function works, it makes sense to have a look at an example. In this simple example we are calculating the body-mass index of a person and then interpreting its value: bmi <- function(mass, size) {
ind <- mass / size^2
switchCase(
  ind,
  alt(
    ..expr <= 15,
    { cat(“Your body mass index is “, ind, ” which is very severely underweight.\n”) },
    “very severely underweight”
  ),
  alt(
    ..expr > 15 & ..expr <= 16,
    { cat(“Your body mass index is “, ind, ” which is severely underweight.\n”) },
  “severely underweight”
  ),
  alt(
    ..expr > 16 & ..expr <= 18.5,
    { cat(“Your body mass index is “, ind, ” which is underweight.\n”) },
    “underweight”
  ),
  alt(
    ..expr > 18.5 & ..expr <= 25,
    { cat(“Your body mass index is “, ind, ” which is normal.\n”) },
    “normal”
  ),
  alt(
    ..expr > 25 & ..expr <= 30,
    { cat(“Your body mass index is “, ind, ” which is overweight.\n”) },
    “overweight”
  ),
  alt(
    ..expr > 30 & ..expr <= 35,
    { cat(“Your body mass index is “, ind, ” which is moderately obese.\n”) },
    “moderately obese”
  ),
  alt(
    ..expr > 35 & ..expr <= 40,
    { cat(“Your body mass index is “, ind, ” which is severely obese.\n”) },
    “severly obese”
  ),
  alt(
    ..expr > 40,
    { cat(“Your body mass index is “, ind, ” which is Very severely obese.\n”) },
    “very severly obese”
  )
 )
}
bmi.person1 <- bmi(82.5, 1.79)
cat(“Person1 turned out to be “, bmi.person1)
Let us go through the call of switchCase() step by step:

All code in the alternative ‘case’ branches is evaluated in the environment from which switchCase() was called so it is easy to access variables of your script or function in which switchCase() is used.
Download the switchcase package from CRAN and visit switchcase‘s GitHub repo on https://github.com/jsugarelli/switchcase.
Follow me on Twitter (https://twitter.com/jsugarelli) to stay up-to-date on new developement around the switchcase package. 

To leave a comment for the author, please follow the link and comment on their blog: Topics in R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.