Answers for "switch statement in r"

R
0

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.
Posted by: Guest on August-22-2020

Browse Popular Code Answers by Language