0
0
R Programmingprogramming~10 mins

Switch statement in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to use the switch statement to print the day name for 1.

R Programming
day <- 1
result <- switch(day, [1], "Tuesday", "Wednesday")
print(result)
Drag options to blanks, or click blank then click option'
A"1"
B1
CMonday
D"Monday"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the day names.
Using numeric values instead of strings for day names.
2fill in blank
medium

Complete the code to use switch with character input to print the correct fruit color.

R Programming
fruit <- "apple"
color <- switch(fruit, apple = [1], banana = "yellow", cherry = "red")
print(color)
Drag options to blanks, or click blank then click option'
Agreen
B"red"
C"green"
Dred
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the color names.
Using the wrong color for apple.
3fill in blank
hard

Fix the error in the switch statement to return the correct output for number 3.

R Programming
num <- 3
result <- switch(num, "one", "two", [1])
print(result)
Drag options to blanks, or click blank then click option'
Athree
B"three"
C"3"
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using numeric values instead of strings.
Forgetting quotes around the string.
4fill in blank
hard

Fill both blanks to create a switch that returns the correct day name for 2 and 4.

R Programming
day <- 4
name <- switch(day, [1], [2], "Wednesday", "Thursday")
print(name)
Drag options to blanks, or click blank then click option'
A"Monday"
B"Tuesday"
C"Wednesday"
D"Thursday"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of days.
Not using quotes for day names.
5fill in blank
hard

Fill all three blanks to create a switch that returns the correct fruit color for "banana".

R Programming
fruit <- "banana"
color <- switch(fruit, apple = [1], banana = [2], cherry = [3])
print(color)
Drag options to blanks, or click blank then click option'
A"green"
B"yellow"
C"red"
D"blue"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong colors for fruits.
Forgetting quotes around color names.