0
0
R Programmingprogramming~15 mins

For loop in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Using a For Loop in R
📖 Scenario: You work in a small bakery and want to calculate the total number of cookies sold in a week. You have the daily sales numbers and want to add them up.
🎯 Goal: Build a simple R program that uses a for loop to sum daily cookie sales and print the total.
📋 What You'll Learn
Create a vector called daily_sales with the exact values: 12, 15, 10, 20, 18, 25, 22
Create a variable called total_sales and set it to 0
Use a for loop with the variable sales to iterate over daily_sales
Add each sales value to total_sales inside the loop
Print the value of total_sales
💡 Why This Matters
🌍 Real World
Summing daily sales is a common task in small businesses to track performance over time.
💼 Career
Understanding loops and data aggregation is essential for data analysis and reporting roles.
Progress0 / 4 steps
1
Create the daily sales data
Create a vector called daily_sales with these exact values: 12, 15, 10, 20, 18, 25, 22
R Programming
Need a hint?

Use the c() function to create a vector with the numbers inside parentheses separated by commas.

2
Create a variable to hold the total sales
Create a variable called total_sales and set it to 0
R Programming
Need a hint?

Just assign 0 to the variable total_sales.

3
Use a for loop to add daily sales
Use a for loop with the variable sales to iterate over daily_sales and add each sales value to total_sales inside the loop
R Programming
Need a hint?

Use for (sales in daily_sales) { ... } and inside add sales to total_sales.

4
Print the total sales
Write print(total_sales) to display the total number of cookies sold in the week
R Programming
Need a hint?

Use the print() function to show the value of total_sales.