Bird
Raised Fist0
R Programmingprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the %in% operator do in R?
easy
A. Calculates the sum of two vectors
B. Performs element-wise multiplication of two vectors
C. Performs matrix multiplication
D. Checks if elements of one vector are present in another vector

Solution

  1. Step 1: Understand the purpose of %in%

    The %in% operator is used to check membership, meaning it tests if elements of one vector exist in another.
  2. Step 2: Compare with other operations

    Other options describe arithmetic or matrix operations, which are not the function of %in%.
  3. Final Answer:

    Checks if elements of one vector are present in another vector -> Option D
  4. Quick Check:

    %in% = membership check [OK]
Hint: Remember: %in% means 'is in' for membership [OK]
Common Mistakes:
  • Confusing %in% with arithmetic operators
  • Thinking %in% does matrix multiplication
  • Assuming %in% returns numeric results
2. Which of the following is the correct syntax for matrix multiplication in R?
easy
A. A * B
B. A %*% B
C. A %in% B
D. A + B

Solution

  1. Step 1: Identify matrix multiplication operator

    In R, matrix multiplication is done using %*%, so A %*% B is correct syntax.
  2. Step 2: Differentiate from other operators

    * is element-wise multiplication, %in% is membership, and + is addition.
  3. Final Answer:

    A %*% B -> Option B
  4. Quick Check:

    Matrix multiply = %*% [OK]
Hint: Use %*% for matrix multiply, * for element-wise [OK]
Common Mistakes:
  • Using * instead of %*% for matrix multiplication
  • Confusing %in% with multiplication
  • Using + for multiplication
3. What is the output of the following R code?
x <- c(3, 1, 5)
y <- c(3, 4, 5, 6)
x %in% y
medium
A. [TRUE, FALSE, TRUE]
B. [FALSE, TRUE, FALSE]
C. [TRUE, TRUE, TRUE]
D. [FALSE, FALSE, FALSE]

Solution

  1. Step 1: Check each element of x in y

    3 is in y, so TRUE; 1 is not in y, so FALSE; 5 is in y, TRUE.
  2. Step 2: Form the logical vector

    The result is [TRUE, FALSE, TRUE].
  3. Final Answer:

    [TRUE, FALSE, TRUE] -> Option A
  4. Quick Check:

    %in% returns logical vector [OK]
Hint: Check each element presence one by one [OK]
Common Mistakes:
  • Mixing order of TRUE/FALSE in output
  • Assuming %in% returns indices
  • Confusing %in% with element-wise comparison
4. Identify the error in the following R code:
A <- matrix(c(1,2,3,4), nrow=2)
B <- matrix(c(5,6,7,8), nrow=2)
C <- A *%* B
medium
A. Matrices A and B have incompatible dimensions
B. Missing library for matrix multiplication
C. Incorrect operator *%* used for matrix multiplication
D. Matrix A is not defined properly

Solution

  1. Step 1: Check operator syntax

    The operator *%* is invalid in R; correct operator is %*%.
  2. Step 2: Verify matrix dimensions and definitions

    Both matrices are 2x2, so dimensions are compatible and matrices are defined correctly.
  3. Final Answer:

    Incorrect operator *%* used for matrix multiplication -> Option C
  4. Quick Check:

    Matrix multiply operator is %*% [OK]
Hint: Use %*% exactly for matrix multiplication [OK]
Common Mistakes:
  • Typing operators in wrong order
  • Assuming *%* is valid
  • Confusing element-wise * with matrix multiply
5. Given two matrices:
M1 <- matrix(c(1, 0, 2, 3), nrow=2)
M2 <- matrix(c(4, 1, 0, 2), nrow=2)

Which R expression correctly computes the matrix product and then checks if the number 5 is in the resulting matrix?
hard
A. 5 %in% (M1 %*% M2)
B. M1 *%*% M2 %in% 5
C. M1 %in% 5 %*% M2
D. M1 %*% M2 == 5

Solution

  1. Step 1: Perform matrix multiplication correctly

    Use %*% to multiply M1 and M2: M1 %*% M2.
  2. Step 2: Check membership of 5 in the product matrix

    Use %in% to check if 5 is present in the resulting matrix: 5 %in% (M1 %*% M2).
  3. Final Answer:

    5 %in% (M1 %*% M2) -> Option A
  4. Quick Check:

    Matrix multiply then membership check = 5 %in% (M1 %*% M2) [OK]
Hint: Chain %*% then %in% with parentheses [OK]
Common Mistakes:
  • Using wrong operator order
  • Typing *%*% instead of %*%
  • Using == instead of %in% for membership
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard