0
0
R Programmingprogramming~30 mins

Apply functions on matrices in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Apply functions on matrices
📖 Scenario: You work in a small bakery that tracks daily sales of different types of bread in a matrix. Each row represents a day, and each column represents a bread type.You want to analyze the sales data by applying functions to the matrix rows and columns.
🎯 Goal: Build an R program that creates a sales matrix, sets a threshold for good sales, applies functions to calculate total sales per day and average sales per bread type, and prints the results.
📋 What You'll Learn
Create a matrix called sales with 3 rows and 4 columns using the exact values: 10, 15, 20, 25, 5, 10, 15, 20, 0, 5, 10, 15
Create a variable called good_sales_threshold and set it to 15
Use apply() to calculate total sales per day (sum of each row) and store it in total_sales_per_day
Use apply() to calculate average sales per bread type (mean of each column) and store it in average_sales_per_bread
Print total_sales_per_day and average_sales_per_bread
💡 Why This Matters
🌍 Real World
Analyzing sales data in a bakery or any small business helps understand daily performance and product popularity.
💼 Career
Data analysts and business managers often use matrix operations and apply functions to summarize and interpret data efficiently.
Progress0 / 4 steps
1
Create the sales matrix
Create a matrix called sales with 3 rows and 4 columns using these exact values in this order: 10, 15, 20, 25, 5, 10, 15, 20, 0, 5, 10, 15
R Programming
Need a hint?

Use the matrix() function with byrow = TRUE to fill rows first.

2
Set the good sales threshold
Create a variable called good_sales_threshold and set it to 15
R Programming
Need a hint?

Just assign the number 15 to the variable good_sales_threshold.

3
Calculate total sales per day and average sales per bread type
Use apply() on the sales matrix to calculate total sales per day (sum of each row) and store it in total_sales_per_day. Then use apply() to calculate average sales per bread type (mean of each column) and store it in average_sales_per_bread.
R Programming
Need a hint?

Use apply(sales, 1, sum) for rows and apply(sales, 2, mean) for columns.

4
Print the results
Print the variables total_sales_per_day and average_sales_per_bread using two separate print() statements.
R Programming
Need a hint?

Use print(total_sales_per_day) and print(average_sales_per_bread).