📖 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
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
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
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
Hint
Use print() to show each value on its own line.
Practice
(1/5)
1. What does the length() function do when applied to a vector in R?
easy
A. It calculates the sum of all elements in the vector.
B. It sorts the elements in the vector.
C. It finds the average value of the elements in the vector.
D. It returns the number of elements in the vector.
Solution
Step 1: Understand the purpose of length()
The length() function counts how many items are inside a vector.
Step 2: Compare with other functions
Unlike sum() or mean(), it does not add or average values, just counts them.
Final Answer:
It returns the number of elements in the vector. -> Option D
2. Which of the following is the correct syntax to calculate the mean of a vector v in R?
easy
A. mean(v)
B. mean = v
C. mean[v]
D. mean v()
Solution
Step 1: Recall function call syntax in R
Functions are called with parentheses and the vector inside, like mean(v).
Step 2: Check each option
mean(v) uses correct syntax. Options B, C, and D are invalid or incorrect syntax.
Final Answer:
mean(v) -> Option A
Quick Check:
Function call uses parentheses: mean(v) [OK]
Hint: Use parentheses to call functions with arguments in R [OK]
Common Mistakes:
Using square brackets instead of parentheses
Assigning mean instead of calling it
Omitting parentheses after function name
3. What is the output of the following R code?
v <- c(2, 4, 6, 8)
sum(v) + length(v)
medium
A. 20
B. 22
C. 24
D. 26
Solution
Step 1: Calculate sum(v)
The vector v is c(2,4,6,8). Sum is 2+4+6+8 = 20.
Step 2: Calculate length(v) and add
Length is number of elements = 4. Adding sum + length = 20 + 4 = 24.
Final Answer:
24 -> Option C
Quick Check:
sum=20 + length=4 = 24 [OK]
Hint: Add sum and length values separately, then combine [OK]
Common Mistakes:
Adding elements incorrectly
Confusing length with sum
Forgetting to add length to sum
4. The following R code is intended to calculate the mean of vector x. What is wrong?
x <- c(5, 10, 15)
mean = mean(x)
print(mean)
medium
A. The print() function cannot print variables.
B. The variable name 'mean' overwrites the function mean(), causing errors later.
C. The vector x is not defined correctly.
D. mean() function requires two arguments.
Solution
Step 1: Analyze variable assignment
Assigning mean = mean(x) overwrites the built-in mean() function with a number.
Step 2: Understand consequences
After this, calling mean() again will cause errors because mean is no longer a function.
Final Answer:
The variable name 'mean' overwrites the function mean(), causing errors later. -> Option B
Quick Check:
Don't name variables same as functions [OK]
Hint: Avoid naming variables same as function names [OK]
Common Mistakes:
Thinking print() cannot print variables
Believing mean() needs two arguments
Ignoring variable shadowing of functions
5. You have a numeric vector nums with some missing values (NA). Which R code correctly calculates the mean ignoring the missing values?
hard
A. mean(nums, na.rm = TRUE)
B. mean(nums, remove.na = TRUE)
C. sum(nums) / length(nums)
D. mean(nums, na.ignore = TRUE)
Solution
Step 1: Understand NA handling in mean()
To ignore missing values, use the argument na.rm = TRUE inside mean().
Step 2: Check each option
mean(nums, na.rm = TRUE) uses correct argument na.rm. Options B and D use incorrect argument names. sum(nums) / length(nums) does not handle NA and will return NA.
Final Answer:
mean(nums, na.rm = TRUE) -> Option A
Quick Check:
Use na.rm=TRUE to ignore NA in mean() [OK]
Hint: Use na.rm=TRUE to skip NA values in mean() [OK]
Common Mistakes:
Using wrong argument names like remove.na or na.ignore
Calculating mean manually without handling NA
Ignoring that NA causes mean() to return NA
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.