Bird
Raised Fist0
R Programmingprogramming~10 mins

Special operators (%in%, %*%) in R Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Special operators (%in%, %*%)
Start
↓
Evaluate left operand
↓
Evaluate right operand
↓
%in%: Check if elements of left are in right
↓
%*%: Perform matrix multiplication
↓
Return result
↓
End
The special operators %in% and %*% take two inputs, evaluate them, then perform membership test or matrix multiplication, and return the result.
Execution Sample
R Programming
x <- c(1, 2, 3)
y <- c(2, 3, 4)
x %in% y
A <- matrix(1:4, 2, 2)
B <- matrix(5:8, 2, 2)
A %*% B
Check which elements of x are in y using %in%, then multiply matrices A and B using %*%.
Execution Table
StepExpressionEvaluationResult
1x <- c(1, 2, 3)Assign vectorx = (1, 2, 3)
2y <- c(2, 3, 4)Assign vectory = (2, 3, 4)
3x %in% yCheck each element of x in y(FALSE, TRUE, TRUE)
4A <- matrix(1:4, 2, 2)Assign matrixA = [[1, 3], [2, 4]]
5B <- matrix(5:8, 2, 2)Assign matrixB = [[5, 7], [6, 8]]
6A %*% BMultiply matrices[[17, 23], [39, 53]]
💡 All expressions evaluated, results returned for %in% and %*%.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4After Step 5Final
xundefined(1, 2, 3)(1, 2, 3)(1, 2, 3)(1, 2, 3)(1, 2, 3)
yundefinedundefined(2, 3, 4)(2, 3, 4)(2, 3, 4)(2, 3, 4)
Aundefinedundefinedundefined[[1, 3], [2, 4]][[1, 3], [2, 4]][[1, 3], [2, 4]]
Bundefinedundefinedundefinedundefined[[5, 7], [6, 8]][[5, 7], [6, 8]]
Key Moments - 2 Insights
Why does x %in% y return a logical vector instead of a single TRUE or FALSE?
Because %in% checks each element of x individually against y, returning TRUE or FALSE for each element as shown in step 3 of the execution_table.
How does %*% differ from the usual * operator in R?
The %*% operator performs matrix multiplication, combining rows and columns, unlike * which does element-wise multiplication. This is shown in step 6 where matrices A and B are multiplied.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the result of x %in% y?
A(FALSE, TRUE, TRUE)
B(TRUE, TRUE, TRUE)
C(TRUE, FALSE, FALSE)
D(FALSE, FALSE, FALSE)
💡 Hint
Check the 'Result' column at step 3 in the execution_table.
At which step does matrix multiplication happen?
AStep 4
BStep 5
CStep 6
DStep 3
💡 Hint
Look for the step where %*% operator is used in the 'Expression' column.
If we change y to c(1, 4, 5), what would be the result of x %in% y at step 3?
A(TRUE, FALSE, TRUE)
B(TRUE, FALSE, FALSE)
C(FALSE, TRUE, TRUE)
D(FALSE, FALSE, FALSE)
💡 Hint
Compare elements of x with new y values and check membership.
Concept Snapshot
Special operators in R:
- %in% checks if elements of left vector are in right vector, returns logical vector.
- %*% performs matrix multiplication between two matrices.
- Use %in% for membership tests, %*% for linear algebra.
- Both require two operands and return a result based on their operation.
Full Transcript
This visual execution shows how R's special operators %in% and %*% work. First, vectors x and y are assigned. Then x %in% y checks each element of x to see if it is in y, returning a logical vector. Next, matrices A and B are assigned. The operator %*% multiplies these matrices using matrix multiplication rules, producing a new matrix. Variables are tracked through each step to show their values. Key moments clarify why %in% returns a vector and how %*% differs from element-wise multiplication. The quiz tests understanding of results and operator behavior.

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