0
0
R Programmingprogramming~15 mins

Useful vector functions (length, sum, mean) in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Useful vector functions (length, sum, mean)
📖 Scenario: You are working with a list of daily sales numbers for a small shop. You want to learn how to use basic R functions to find out how many days you have data for, the total sales, and the average sales per day.
🎯 Goal: Build a simple R script that uses the length(), sum(), and mean() functions on a vector of sales numbers to get useful information.
📋 What You'll Learn
Create a numeric vector called sales with exact values: 150, 200, 175, 300, 225
Create a variable called total_days that stores the length of the sales vector using length()
Create a variable called total_sales that stores the sum of the sales vector using sum()
Create a variable called average_sales that stores the mean of the sales vector using mean()
Print the values of total_days, total_sales, and average_sales each on its own line
💡 Why This Matters
🌍 Real World
Small business owners often track daily sales to understand their performance. Using R functions like length, sum, and mean helps quickly summarize sales data.
💼 Career
Data analysts and business intelligence professionals use these basic vector functions to prepare and analyze data before creating reports or visualizations.
Progress0 / 4 steps
1
Create the sales vector
Create a numeric vector called sales with these exact values: 150, 200, 175, 300, 225
R Programming
Need a hint?

Use the c() function to combine numbers into a vector.

2
Calculate total days
Create a variable called total_days that stores the length of the sales vector using the length() function
R Programming
Need a hint?

The length() function tells you how many elements are in a vector.

3
Calculate total and average sales
Create a variable called total_sales that stores the sum of the sales vector using sum(), and a variable called average_sales that stores the mean of the sales vector using mean()
R Programming
Need a hint?

Use sum() to add all numbers and mean() to find the average.

4
Print the results
Print the values of total_days, total_sales, and average_sales each on its own line using three separate print() statements
R Programming
Need a hint?

Use print() to show each value on its own line.