Tuesday, April 18, 2023

R Tutorial Playlist - Switch case

 Switch Case statement is designed to serve the same purpose as If Else statement but with focus on cases with bigger number of conditions. There is no actual limit where you should stop using one and start using another; it's on every developer to decide that for himself.

a <- 2

switch(

    a,

    print("1"),

    print("2"),

    print("3")

)

As you can see, switch case statement in R is functions where first argument is value that is going to be tested. In our case value a is holding numeric value of 2. R checks value stored in first argument and then it executes statement with that index. Indexing is started from 1, from second argument provided to switch function. 

a <-"2"

switch(

    a,

    "1"= print("1"),

    "2"= print("2"),

    "3"= print("3")

)

In second case, a holds textual value. Now, every statement checks is the value at the beginning of every statement equals to value holded by variable a. If it is, that statement is going to be executed.

Check video tutorial below.



No comments:

Post a Comment