Apply Family Functions vs Loops in R
📖 Scenario: You work in a small bakery that tracks daily sales of different bread types. You want to calculate the total sales for each bread type over a week.
🎯 Goal: Build a program that calculates total weekly sales for each bread type using both a for loop and the lapply function, then compare the results.
📋 What You'll Learn
Create a list called
daily_sales with 3 named numeric vectors representing sales for 'Sourdough', 'Baguette', and 'Rye' over 7 daysCreate a numeric vector called
total_sales_loop to store total sales for each bread type using a for loopCreate a numeric vector called
total_sales_apply to store total sales for each bread type using lapplyPrint both
total_sales_loop and total_sales_apply to compare results💡 Why This Matters
🌍 Real World
Bakeries and small shops often track daily sales data and need to summarize totals quickly to understand performance.
💼 Career
Knowing how to use loops and apply functions in R is essential for data analysts and anyone working with data summaries in real-world business settings.
Progress0 / 4 steps