0
0
R Programmingprogramming~20 mins

Special operators (%in%, %*%) in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Special Operators Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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% f2
R Programming
f1 <- factor(c("apple", "banana", "cherry"))
f2 <- factor(c("banana", "date", "apple"))
f1 %in% f2
A[1] TRUE TRUE FALSE
B[1] FALSE TRUE TRUE
C[1] TRUE FALSE TRUE
D[1] FALSE FALSE FALSE
Attempts:
2 left
💡 Hint
Remember that %in% checks if elements of the left vector are present in the right vector, comparing levels for factors.
Predict Output
intermediate
2: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
A
     [,1] [,2]
[1,]   23   31
[2,]   34   46
B
     [,1] [,2]
[1,]   23   34
[2,]   31   46
C
     [,1] [,2]
[1,]   26   30
[2,]   38   44
D
     [,1] [,2]
[1,]   17   20
[2,]   39   46
Attempts:
2 left
💡 Hint
Recall matrix multiplication rules: multiply rows of A by columns of B and sum.
Predict Output
advanced
2: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
A[1] FALSE TRUE FALSE FALSE
B[1] FALSE TRUE TRUE FALSE
C[1] FALSE TRUE NA FALSE
D[1] FALSE TRUE NA NA
Attempts:
2 left
💡 Hint
Think about how NA values are treated in logical comparisons with %in%.
Predict Output
advanced
2: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
AError in A %*% B : missing values in matrix
BError in A %*% B : object 'B' not found
CError in A %*% B : invalid matrix multiplication
DError in A %*% B : non-conformable arguments
Attempts:
2 left
💡 Hint
Check the dimensions of matrices for multiplication compatibility.
Predict Output
expert
3: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)
A
       a     b
[1,] TRUE TRUE
[2,] FALSE TRUE
[3,] FALSE FALSE
B
       a     b
[1,] TRUE FALSE
[2,] FALSE TRUE
[3,] FALSE FALSE
C
       a     b
[1,] TRUE FALSE
[2,] TRUE TRUE
[3,] FALSE FALSE
D
       a     b
[1,] TRUE FALSE
[2,] FALSE TRUE
[3,] TRUE FALSE
Attempts:
2 left
💡 Hint
Check each element of v against each list element using %in%, then sapply returns a matrix.