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 matrixCreate a numeric matrix called
prices with values c(2, 3) as a 2-row, 1-column matrixUse
%in% to check which sold products are in inventoryUse
%*% to multiply sales_qty by prices to get total salesPrint 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