0
0
R Programmingprogramming~10 mins

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

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