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.
Comparing the performance of switch and series of the if…else statements
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...