Challenge - 5 Problems
Special Operators Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of %in% operator with factors
What is the output of this R code?
f1 <- factor(c("apple", "banana", "cherry"))
f2 <- factor(c("banana", "date", "apple"))
f1 %in% f2R Programming
f1 <- factor(c("apple", "banana", "cherry")) f2 <- factor(c("banana", "date", "apple")) f1 %in% f2
Attempts:
2 left
💡 Hint
Remember that %in% checks if elements of the left vector are present in the right vector, comparing levels for factors.
✗ Incorrect
The %in% operator returns a logical vector indicating if each element of f1 is found in f2. 'apple' and 'banana' are in f2, but 'cherry' is not.
❓ Predict Output
intermediate2:00remaining
Matrix multiplication with %*%
What is the result of this R code?
A <- matrix(c(1, 2, 3, 4), nrow=2) B <- matrix(c(5, 6, 7, 8), nrow=2) A %*% B
R Programming
A <- matrix(c(1, 2, 3, 4), nrow=2) B <- matrix(c(5, 6, 7, 8), nrow=2) A %*% B
Attempts:
2 left
💡 Hint
Recall matrix multiplication rules: multiply rows of A by columns of B and sum.
✗ Incorrect
The product matrix is calculated as: element (1,1) = 1*5 + 3*6 = 23, element (1,2) = 1*7 + 3*8 = 31, element (2,1) = 2*5 + 4*6 = 34, element (2,2) = 2*7 + 4*8 = 46.
❓ Predict Output
advanced2:00remaining
Behavior of %in% with NA values
What is the output of this R code?
x <- c(1, 2, NA, 4) y <- c(2, NA, 5) x %in% y
R Programming
x <- c(1, 2, NA, 4) y <- c(2, NA, 5) x %in% y
Attempts:
2 left
💡 Hint
Think about how NA values are treated in logical comparisons with %in%.
✗ Incorrect
The %in% operator returns NA when comparing NA to NA because NA means unknown, so it cannot confirm presence or absence. So the third element is NA.
❓ Predict Output
advanced2:00remaining
Matrix multiplication dimension mismatch error
What error does this R code produce?
A <- matrix(1:6, nrow=2) B <- matrix(1:6, nrow=2) A %*% B
R Programming
A <- matrix(1:6, nrow=2) B <- matrix(1:6, nrow=2) A %*% B
Attempts:
2 left
💡 Hint
Check the dimensions of matrices for multiplication compatibility.
✗ Incorrect
Matrix multiplication requires the number of columns in A to equal the number of rows in B. A is 2x3, B is 2x3, so columns of A (3) does not match rows of B (2), producing 'Error in A %*% B : non-conformable arguments'.
❓ Predict Output
expert3:00remaining
Complex use of %in% with lists and vectors
What is the output of this R code?
lst <- list(a = 1:3, b = 4:6) v <- c(2, 5, 7) sapply(lst, function(x) v %in% x)
R Programming
lst <- list(a = 1:3, b = 4:6) v <- c(2, 5, 7) sapply(lst, function(x) v %in% x)
Attempts:
2 left
💡 Hint
Check each element of v against each list element using %in%, then sapply returns a matrix.
✗ Incorrect
For list element 'a' (1:3), 2 is in it (TRUE), 5 and 7 are not (FALSE). For 'b' (4:6), 5 is in it (TRUE), 2 and 7 are not (FALSE). So the matrix shows TRUE/FALSE accordingly.