0
0
R Programmingprogramming~15 mins

Switch statement in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Switch Statement in R
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use the print() function to show the value of day_name.