Comparing the performance of switch and series of the if…else statements
Sometimes, you are in a situation where you will execute one task from a list of alternatives based on a conditional value of another variable. In this case, you have two ways of doing it, either using the switch
function or a series of the if…else if…else
statement. The question is which one performs better. In this recipe, you will assess the performance of these two alternatives.
Getting ready
You are given a vector of numeric values representing the number of hours spent on social media in a week as follows:
13, 21, 19, 18, 21, 16, 21, 24, 17, 18, 12, 18, 29, 17, 18, 11, 13, 20, 25, 18, 15, 19, 21, 21, 7, 12, 23, 31, 16, 19, 23, 15, 25, 19, 15, 25, 25, 16, 29, 15, 26, 29, 23, 24, 20, 19, 14, 27, 22, 26
The task is to calculate the mean, standard deviation, median, and MAD based on a value of another variable sym
. If the value of the sym
variable is classical
, then you will calculate the mean and standard...