The %in% operator checks if elements of one vector are present in another vector. It returns a logical vector of TRUE or FALSE values.
Click to reveal answer
beginner
How does the %*% operator work in R?
The %*% operator performs matrix multiplication between two matrices or vectors, following the rules of linear algebra.
Click to reveal answer
beginner
Example: What is the result of c(1, 2, 3) %in% c(2, 3, 4)?
The result is a logical vector: FALSE TRUE TRUE. It shows which elements of the first vector are found in the second.
Click to reveal answer
intermediate
Example: What is the output of matrix(c(1,2,3,4), nrow=2) %*% matrix(c(5,6,7,8), nrow=2)?
The output is a 2x2 matrix:
[,1] [,2]
[1,] 19 22
[2,] 43 50
This is the result of multiplying the two matrices using matrix multiplication rules.
Click to reveal answer
intermediate
Can %in% be used with data frames or lists?
Yes, %in% can be used to check if elements exist in vectors extracted from data frames or lists, but it works element-wise on vectors, not directly on data frames.
Click to reveal answer
What does the expression '3 %in% c(1, 2, 3, 4)' return in R?
ATRUE
BFALSE
C3
DError
✗ Incorrect
3 is present in the vector c(1, 2, 3, 4), so %in% returns TRUE.
Which operator is used for matrix multiplication in R?
A*
B%*%
C%in%
D%%
✗ Incorrect
The %*% operator performs matrix multiplication in R.
What is the output type of 'c(1,2,3) %in% c(2,3,4)'?
ANumeric vector
BMatrix
CLogical vector
DCharacter vector
✗ Incorrect
%in% returns a logical vector indicating presence or absence.
If A is a 2x3 matrix and B is a 3x2 matrix, what does A %*% B produce?
AError
BA 3x3 matrix
CA 2x3 matrix
DA 2x2 matrix
✗ Incorrect
Matrix multiplication of 2x3 and 3x2 matrices results in a 2x2 matrix.
Can %in% be used to check if a value is in a list directly?
ANo, it only works on vectors
BYes, it works directly on lists
CYes, but only for numeric lists
DNo, it only works on matrices
✗ Incorrect
%in% works element-wise on vectors, not directly on lists.
Explain how the %in% operator works and give a simple example.
Think about checking if items are inside a shopping list.
You got /3 concepts.
Describe the purpose of the %*% operator and what kind of data it works with.
Imagine multiplying two tables of numbers to get a new table.
You got /3 concepts.
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
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.
Step 2: Compare with other operations
Other options describe arithmetic or matrix operations, which are not the function of %in%.
Final Answer:
Checks if elements of one vector are present in another vector -> Option D
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
Step 1: Identify matrix multiplication operator
In R, matrix multiplication is done using %*%, so A %*% B is correct syntax.
Step 2: Differentiate from other operators
* is element-wise multiplication, %in% is membership, and + is addition.
Final Answer:
A %*% B -> Option B
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
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.
Step 2: Form the logical vector
The result is [TRUE, FALSE, TRUE].
Final Answer:
[TRUE, FALSE, TRUE] -> Option A
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
Step 1: Check operator syntax
The operator *%* is invalid in R; correct operator is %*%.
Step 2: Verify matrix dimensions and definitions
Both matrices are 2x2, so dimensions are compatible and matrices are defined correctly.
Final Answer:
Incorrect operator *%* used for matrix multiplication -> Option C
Quick Check:
Matrix multiply operator is %*% [OK]
Hint: Use %*% exactly for matrix multiplication [OK]