📖 Scenario: You are working with a small store's inventory and sales data. You want to check which products sold are in the inventory and calculate total sales using matrix multiplication.
🎯 Goal: Build a simple R script that uses the %in% operator to check product membership and the %*% operator to multiply sales quantities by prices.
📋 What You'll Learn
Create a character vector called inventory with product names: "apple", "banana", "orange"
Create a character vector called sold with product names: "banana", "kiwi"
Create a numeric matrix called sales_qty with values c(10, 5) as a 1-row matrix
Create a numeric matrix called prices with values c(2, 3) as a 2-row, 1-column matrix
Use %in% to check which sold products are in inventory
Use %*% to multiply sales_qty by prices to get total sales
Print the logical vector result of membership check
Print the total sales value
💡 Why This Matters
🌍 Real World
Checking product availability and calculating sales totals are common tasks in retail and inventory management.
💼 Career
Understanding these operators helps in data analysis roles, especially when working with product data and sales reports.
Progress0 / 4 steps
1
Create product vectors
Create a character vector called inventory with these exact products: "apple", "banana", "orange". Also create a character vector called sold with these exact products: "banana", "kiwi".
R Programming
Hint
Use the c() function to create vectors with exact product names.
2
Create sales quantity and price matrices
Create a numeric matrix called sales_qty with values c(10, 5) arranged as 1 row and 2 columns. Create a numeric matrix called prices with values c(2, 3) arranged as 2 rows and 1 column.
R Programming
Hint
Use the matrix() function with nrow and ncol to create matrices.
3
Check sold products in inventory and calculate total sales
Use the %in% operator to create a logical vector called in_inventory that checks which products in sold are also in inventory. Use the %*% operator to multiply sales_qty by prices and store the result in total_sales.
R Programming
Hint
Use %in% between sold and inventory. Use %*% between sales_qty and prices.
4
Print the results
Print the logical vector in_inventory to show which sold products are in inventory. Then print the numeric value total_sales to show total sales amount.
R Programming
Hint
Use print() to show the values of in_inventory and total_sales.
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]