0
0
R Programmingprogramming~20 mins

Comparison operators in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Comparison Operators Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[1] TRUE TRUE TRUE TRUE
B[1] TRUE FALSE TRUE FALSE
C[1] FALSE FALSE TRUE TRUE
D[1] FALSE TRUE TRUE TRUE
Attempts:
2 left
💡 Hint
Remember that R recycles shorter vectors to match the length of longer vectors.
Predict Output
intermediate
2:00remaining
Output of chained comparison operators
What is the output of this R code?
5 > 3 & 2 < 4
R Programming
5 > 3 & 2 < 4
A[1] TRUE
B[1] FALSE
C[1] TRUE TRUE
D[1] FALSE FALSE
Attempts:
2 left
💡 Hint
The & operator checks if both conditions are TRUE.
Predict Output
advanced
2: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
A[1] FALSE NA TRUE
B[1] FALSE FALSE TRUE
C[1] TRUE NA TRUE
D[1] NA NA TRUE
Attempts:
2 left
💡 Hint
Comparisons with NA produce NA unless the value is known to be equal or not.
Predict Output
advanced
2: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 == f2
R Programming
f1 <- factor(c("a", "b"))
f2 <- factor(c("b", "a"))
f1 == f2
A[1] FALSE TRUE
B[1] TRUE TRUE
C[1] TRUE FALSE
D[1] FALSE FALSE
Attempts:
2 left
💡 Hint
Factors compare underlying integer codes, not just labels.
🧠 Conceptual
expert
3:00remaining
Behavior of comparison operators with matrices
Given two matrices:
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
A
     [,1]  [,2]
[1,] FALSE FALSE
[2,]  TRUE  TRUE
B
     [,1]  [,2]
[1,] FALSE  TRUE
[2,]  TRUE  TRUE
C
     [,1]  [,2]
[1,]  TRUE FALSE
[2,] FALSE  TRUE
D
     [,1]  [,2]
[1,]  TRUE  TRUE
[2,] FALSE FALSE
Attempts:
2 left
💡 Hint
Comparison is element-wise between corresponding elements of matrices.