0
0
R Programmingprogramming~10 mins

Ifelse vectorized function 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 ifelse to assign 'pass' if score is 50 or more, else 'fail'.

R Programming
result <- ifelse(score [1] 50, 'pass', 'fail')
Drag options to blanks, or click blank then click option'
A==
B<
C>=
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>=' causes wrong results.
Using '==' only checks for exactly 50, not more.
2fill in blank
medium

Complete the code to assign 'even' if number is divisible by 2, else 'odd'.

R Programming
result <- ifelse(number %% 2 [1] 0, 'even', 'odd')
Drag options to blanks, or click blank then click option'
A!=
B<
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' reverses the logic, marking even numbers as odd.
Using '>' or '<' does not correctly check divisibility.
3fill in blank
hard

Fix the error in the ifelse condition to correctly assign 'adult' if age is 18 or more.

R Programming
category <- ifelse(age [1] 18, 'adult', 'minor')
Drag options to blanks, or click blank then click option'
A>=
B!=
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' assigns adults incorrectly.
Using '==' only matches exactly 18, missing older ages.
4fill in blank
hard

Fill both blanks to create a vectorized ifelse that labels numbers as 'positive' if greater than zero, else 'non-positive'.

R Programming
labels <- ifelse(numbers [1] 0, 'positive', [2])
Drag options to blanks, or click blank then click option'
A>
B'non-positive'
C'negative'
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' in the condition reverses logic.
Using 'negative' instead of 'non-positive' changes meaning.
5fill in blank
hard

Fill all three blanks to create a vectorized ifelse that labels temperatures as 'hot' if above 30, 'cold' if below 15, else 'warm'.

R Programming
labels <- ifelse(temp [1] 30, 'hot', ifelse(temp [2] 15, 'cold', [3]))
Drag options to blanks, or click blank then click option'
A>
B<
C'warm'
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' instead of '>' changes the boundary.
Using wrong labels in the else part.