5

In R, is there a way to make a switch statement such that the same block of code is executed for two different cases? Obviously I could copy and paste the whole body of code for both statements, but I was hoping there would be a neater way to do it.

I could also use an if-else block to avoid the repetition of large blocks of code but switches are generally faster in R.

It seems unlikely due to the way R parses a switch statement as a function, but I'm hoping that the developers of R took special care in parsing a switch statement to allow for multiple arguments to refer to the same block of code.

Jon Claus
  • 2,862
  • 4
  • 22
  • 33
  • The way you talk about the function `switch` doesn't make much sense to me, can you provide a small example please that illustrates what you're trying to do? – joran Jun 14 '13 at 17:00
  • Not sure I follow, but creating a function with your desired block of code would accomplish this in a straightforward and R-ish way. – Justin Jun 14 '13 at 17:02
  • @joran he wants something similar to C's switch, which has "fall-through" behaviour unless an explicit `break` is added. IIRC, this is usually considered a bad thing, although people have coded up some fiendish hacks with it. Also, {{citation needed}} on the assertion that switch is faster than if. – Hong Ooi Jun 14 '13 at 17:03
  • @HongOoi So that sounds like this has nothing to do with the `switch` function specifically, then, it seems, which is much more limited...? – joran Jun 14 '13 at 17:05
  • The citation for the speed: http://stackoverflow.com/questions/7825501/switch-statement-usage – Jon Claus Jun 14 '13 at 17:08
  • @joran, the keyword `switch` in R is treated as a function. It's parse tree contains the name of the function call in the first element (`switch` in this case), the expression as the second argument, the rest of the elements comprise a named list with the names being the values of expression (also `language` objects) with the last element being the unnamed default case (if there is one). The values in the named list part of the expression are null if not specified. For example, try `as.list(substitute(switch(expr, 'case1' =, 'case2' = {code}, {default})))` verbatim. – Jon Claus Jun 14 '13 at 17:16
  • Yes, yes, I was just confused by the language you were using. Because I think of switch as a function, I don't think about it as "executing blocks of code" as alternatives, so much as evaluating an expression and then picking a matching value. Semantics. – joran Jun 14 '13 at 17:20

1 Answers1

7

Provide named arguments without values, they fall through to the next expression with value

> switch("A", A=, B=, C="A OR B OR C", "Other")
[1] "A OR B OR C"
> switch("C", A=, B=, C="A OR B OR C", "Other")
[1] "A OR B OR C"
> switch("D", A=, B=, C="A OR B OR C", "Other")
[1] "Other"

This is described on the help page ?switch

 If 'EXPR' evaluates to a character string then that string is
 matched (exactly)to the names of the elements in '...'.  If there
 is a match then that element is evaluated unless it is missing, in
 which case the next non-missing element is evaluated, so for
 example 'switch("cc", a = 1, cc =, cd =, d = 2)' evaluates to '2'.
Martin Morgan
  • 45,935
  • 7
  • 84
  • 112
  • 1
    Thanks. I assumed they didn't work like that because in Java you need the explicit `break` to make so that they don't fall through to the next expression; the lack of breaks led me to believe that there always no fall through in R. – Jon Claus Jun 14 '13 at 17:06