switch r code example
Example: switch statement in r
#switch statement in R
test_switch <- function(x){
switch(x,
a = "dog",
b = , #the output to "b" falls to the next value
c = "cat",
stop("Invalid input."))
}
test_switch("a")
>> [1] "dog"
test_switch("b")
>> [1] "cat"
test_switch("c")
>> [1] "cat"
test_switch("everything else")
>> Error in test_switch("everything else") : Invalid input.