📖 Scenario: You are creating a simple program to convert a number representing a day of the week into the day's name. This is useful for apps that show schedules or reminders.
🎯 Goal: Build a program that uses the switch() statement in R to print the correct day name for a given day number.
📋 What You'll Learn
Create a variable day_number with a value from 1 to 7
Create a variable default_day with the value "Invalid day"
Use the switch() function with day_number to assign the correct day name to day_name
Print the value of day_name
💡 Why This Matters
🌍 Real World
Switch statements help programs choose actions based on user input or data, like showing the correct day name for a calendar app.
💼 Career
Understanding switch statements is useful for writing clear and efficient code in data analysis, reporting, and software development.
Progress0 / 4 steps
1
Create the day number variable
Create a variable called day_number and set it to 3.
R Programming
Hint
Use the assignment operator <- to assign the value 3 to day_number.
2
Create the default day variable
Create a variable called default_day and set it to the string "Invalid day".
R Programming
Hint
Remember to use quotes around the string value.
3
Use switch() to assign the day name
Use the switch() function with day_number to assign the correct day name to a variable called day_name. Use these mappings: 1 = "Monday", 2 = "Tuesday", 3 = "Wednesday", 4 = "Thursday", 5 = "Friday", 6 = "Saturday", 7 = "Sunday". Use default_day as the default value if day_number is not 1 to 7.
R Programming
Hint
Convert day_number to a character string inside switch() because switch() matches strings in R.
4
Print the day name
Print the value of the variable day_name.
R Programming
Hint
Use the print() function to show the value of day_name.
Practice
(1/5)
1. What does the switch() function in R primarily do?
easy
A. Selects a value from multiple options based on a given expression
B. Repeats a block of code multiple times
C. Defines a new function
D. Creates a loop that runs until a condition is false
Solution
Step 1: Understand the purpose of switch()
The switch() function evaluates an expression and returns a value based on matching cases.
Step 2: Compare with other options
Options A, C, and D describe loops or function definitions, which are not the role of switch().
Final Answer:
Selects a value from multiple options based on a given expression -> Option A
Quick Check:
Switch chooses one option based on input [OK]
Hint: Switch picks one matching case value quickly [OK]
Common Mistakes:
Confusing switch with loops like for or while
Thinking switch defines functions
Missing the idea of selecting based on expression
2. Which of the following is the correct syntax to use switch() in R?
D. switch(expression, case1: value1, case2: value2, default: value3)
Solution
Step 1: Recall R's switch syntax
In R, switch() uses named arguments for cases like case1 = value1.
Step 2: Check each option
switch(expression, case1 = value1, case2 = value2, default = value3) matches R syntax. Options A, B, and C use syntax styles from other languages or incorrect separators.
result <- switch("b", a = 10, b = 20, c = 30, 40)
print(result)
medium
A. 10
B. 20
C. 30
D. 40
Solution
Step 1: Identify the switch expression and cases
The expression is "b". The cases are a=10, b=20, c=30, and a default 40.
Step 2: Match the expression to a case
Since the expression matches case "b", the value 20 is returned.
Final Answer:
20 -> Option B
Quick Check:
Expression "b" matches case b = 20 [OK]
Hint: Match expression string to named case [OK]
Common Mistakes:
Choosing default value instead of matching case
Confusing case names with values
Forgetting switch returns matched case value
4. Identify the error in this R switch statement:
switch(2,
1 = "one",
2 = "two",
3 = "three"
)
medium
A. Switch expression should be a string, not a number
B. Missing a default case
C. Incorrect use of commas between cases
D. Using numeric cases with = instead of unnamed cases
Solution
Step 1: Understand switch with numeric expression in R
When the expression is numeric, switch expects unnamed arguments, not named with =.
Step 2: Check the code
The code uses named cases like 1 = "one", which is invalid syntax for numeric switch.
Final Answer:
Using numeric cases with = instead of unnamed cases -> Option D
Quick Check:
Numeric switch uses unnamed cases, not named with = [OK]
Hint: Use unnamed cases for numeric switch, named for string [OK]
Common Mistakes:
Using = with numeric cases causes syntax error
Assuming default case is mandatory
Thinking switch expression must be string
5. You want to write a switch statement in R that returns "small", "medium", or "large" for numeric input 1, 2, or 3 respectively, and "unknown" for any other number. Which code correctly implements this?
Step 1: Understand switch behavior with numeric input
For numeric input matching positions 1,2,3, use unnamed positional arguments, placing "unknown" as the 4th for other small numbers.
Step 2: Analyze each option
A, C use string literals like '1' = which cause syntax error (unexpected =). D uses numeric literals 1 = which cause syntax error (unexpected =). B uses unnamed arguments, correct for numeric positional matching.
Final Answer:
switch(x, "small", "medium", "large", "unknown") -> Option C
Quick Check:
Numeric switch uses unnamed positional args [OK]
Hint: Unnamed args for numeric switch positions [OK]
Common Mistakes:
Using numeric cases with = causes syntax error
Not providing a default value for unmatched cases
Mixing numeric expression with string case names
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.