0
0
R Programmingprogramming~20 mins

Ifelse vectorized function in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ifelse Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of ifelse with numeric vector
What is the output of the following R code using ifelse?
R Programming
x <- c(2, 5, 8, 1)
result <- ifelse(x > 4, x * 2, x + 1)
print(result)
A[1] 3 10 16 2
B[1] 2 5 8 1
C[1] 3 5 8 2
D[1] 4 10 16 2
Attempts:
2 left
💡 Hint
Remember that ifelse applies the condition element-wise and returns a vector.
🧠 Conceptual
intermediate
2:00remaining
Understanding ifelse with NA values
What will be the output of this R code using ifelse with NA values?
R Programming
x <- c(NA, 3, 7, NA)
result <- ifelse(is.na(x), 0, x)
print(result)
A[1] 0 3 7 0
B[1] NA 3 7 NA
C[1] 0 NA NA 0
D[1] 0 0 0 0
Attempts:
2 left
💡 Hint
Check how is.na(x) returns TRUE or FALSE for each element.
🔧 Debug
advanced
2:00remaining
Identify the error in ifelse usage
What error does the following R code produce?
R Programming
x <- c(1, 2, 3)
result <- ifelse(x > 2, "High", 0)
print(result)
AError: non-numeric argument to binary operator
BWarning: Incompatible types in ifelse condition
CNo error; output is [1] "0" "0" "High"
DNo error; output is [1] 0 0 "High"
Attempts:
2 left
💡 Hint
Check how R handles mixed types in ifelse output vectors.
📝 Syntax
advanced
2:00remaining
Syntax error in ifelse usage
Which option contains a syntax error in using ifelse in R?
Aifelse(x > 0, "Positive", "Negative")
Bifelse(x > 0 "Positive", "Negative")
C)"evitageN" ,"evitisoP" ,0 > x(eslefi
Dfelse(x > 0, "Positive", "Negative")
Attempts:
2 left
💡 Hint
Look for missing commas or parentheses.
🚀 Application
expert
3:00remaining
Using ifelse to categorize numeric vector
Given scores <- c(45, 67, 89, 55, 72), which code correctly assigns grades: "Fail" for scores below 50, "Pass" for 50 to 75, and "Distinction" for above 75?
R Programming
scores <- c(45, 67, 89, 55, 72)
Agrades <- ifelse(scores < 50, "Fail", ifelse(scores >= 75, "Pass", "Distinction"))
Bgrades <- ifelse(scores < 50, "Fail", ifelse(scores > 75, "Distinction", "Pass"))
Cgrades <- ifelse(scores <= 50, "Fail", ifelse(scores < 75, "Pass", "Distinction"))
Dgrades <- ifelse(scores < 50, "Fail", ifelse(scores <= 75, "Pass", "Distinction"))
Attempts:
2 left
💡 Hint
Think about the order of conditions and inclusive ranges.