Challenge - 5 Problems
Comparison Operators Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of vector comparison with recycling
What is the output of this R code?
c(1, 2, 3, 4) > c(2, 2)
R Programming
c(1, 2, 3, 4) > c(2, 2)
Attempts:
2 left
💡 Hint
Remember that R recycles shorter vectors to match the length of longer vectors.
✗ Incorrect
The shorter vector c(2, 2) is recycled to c(2, 2, 2, 2). Then element-wise comparison is done: 1>2 is FALSE, 2>2 is FALSE, 3>2 is TRUE, 4>2 is TRUE.
❓ Predict Output
intermediate2:00remaining
Output of chained comparison operators
What is the output of this R code?
5 > 3 & 2 < 4
R Programming
5 > 3 & 2 < 4
Attempts:
2 left
💡 Hint
The & operator checks if both conditions are TRUE.
✗ Incorrect
5 > 3 is TRUE and 2 < 4 is TRUE, so TRUE & TRUE returns TRUE.
❓ Predict Output
advanced2:00remaining
Output of comparison with NA values
What is the output of this R code?
c(1, NA, 3) == 3
R Programming
c(1, NA, 3) == 3
Attempts:
2 left
💡 Hint
Comparisons with NA produce NA unless the value is known to be equal or not.
✗ Incorrect
1 == 3 is FALSE, NA == 3 is NA (unknown), 3 == 3 is TRUE.
❓ Predict Output
advanced2:00remaining
Output of comparing factors with different levels
What is the output of this R code?
f1 <- factor(c("a", "b"))
f2 <- factor(c("b", "a"))
f1 == f2R Programming
f1 <- factor(c("a", "b")) f2 <- factor(c("b", "a")) f1 == f2
Attempts:
2 left
💡 Hint
Factors compare underlying integer codes, not just labels.
✗ Incorrect
f1 has levels a=1, b=2 so codes 1,2. f2 has levels b=1, a=2 so first element 'b' (code 1), second 'a' (code 2), codes 1,2. Thus 1==1 TRUE, 2==2 TRUE.
🧠 Conceptual
expert3:00remaining
Behavior of comparison operators with matrices
Given two matrices:
What is the output of
m1 <- matrix(1:4, nrow=2) m2 <- matrix(c(2, 2, 3, 3), nrow=2)
What is the output of
m1 >= m2?R Programming
m1 <- matrix(1:4, nrow=2) m2 <- matrix(c(2, 2, 3, 3), nrow=2) m1 >= m2
Attempts:
2 left
💡 Hint
Comparison is element-wise between corresponding elements of matrices.
✗ Incorrect
m1 is matrix:
[,1] [,2]
[1,] 1 3
[2,] 2 4
m2 is matrix:
[,1] [,2]
[1,] 2 3
[2,] 2 3
Comparisons:
1 >= 2 is FALSE
3 >= 3 is TRUE
2 >= 2 is TRUE
4 >= 3 is TRUE
So output matrix is:
[,1] [,2]
[1,] FALSE TRUE
[2,] TRUE TRUE