0
0
R Programmingprogramming~30 mins

Special operators (%in%, %*%) in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Special Operators %in% and %*% in R
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print() to show the values of in_inventory and total_sales.