📖 Scenario: You work as a data analyst for a small company. You have collected sales data for a week and want to understand the basic statistics like average sales, minimum, and maximum sales to help the team make decisions.
🎯 Goal: Build a simple R script that calculates summary statistics (mean, minimum, maximum) for daily sales data.
📋 What You'll Learn
Create a numeric vector with exact daily sales values
Create a variable to hold the number of days
Calculate mean, minimum, and maximum sales using built-in functions
Print the calculated summary statistics
💡 Why This Matters
🌍 Real World
Summary statistics help businesses quickly understand data like sales, customer visits, or production numbers.
💼 Career
Data analysts and scientists use summary statistics daily to describe data before deeper analysis.
Progress0 / 4 steps
1
Create the sales data vector
Create a numeric vector called sales with these exact daily sales values: 150, 200, 170, 220, 180, 210, 190.
R Programming
Hint
Use the c() function to combine numbers into a vector.
2
Create a variable for the number of days
Create a variable called num_days that stores the length of the sales vector using the length() function.
R Programming
Hint
The length() function returns how many elements are in a vector.
3
Calculate summary statistics
Calculate the mean of sales and store it in avg_sales. Calculate the minimum and maximum sales and store them in min_sales and max_sales respectively. Use the functions mean(), min(), and max().
R Programming
Hint
Use mean(), min(), and max() functions on the sales vector.
4
Print the summary statistics
Print the variables avg_sales, min_sales, and max_sales each on a new line using the print() function.
R Programming
Hint
Use print() to show each statistic on its own line.
Practice
(1/5)
1. What does the summary() function in R provide when applied to a numeric vector?
easy
A. Minimum, 1st quartile, median, mean, 3rd quartile, and maximum values
B. Only the mean and median values
C. The sum and product of all elements
D. The count of unique values
Solution
Step 1: Understand what summary() does for numeric vectors
The summary() function returns key statistics including minimum, quartiles, median, mean, and maximum.
Step 2: Compare options with the known output
Minimum, 1st quartile, median, mean, 3rd quartile, and maximum values lists all these statistics correctly, while others miss important parts or give unrelated info.
Final Answer:
Minimum, 1st quartile, median, mean, 3rd quartile, and maximum values -> Option A
Quick Check:
summary() = min, Q1, median, mean, Q3, max [OK]
Hint: Remember summary() shows min, quartiles, median, mean, max [OK]
Common Mistakes:
Thinking summary() only gives mean and median
Confusing summary() with sum() or length()
Expecting summary() to count unique values
2. Which of the following is the correct way to get summary statistics of a data frame named df in R?
easy
A. stats(df)
B. summarize(df)
C. summary(df)
D. summary_stats(df)
Solution
Step 1: Recall the correct function name for summary statistics in R
The built-in function to get summary statistics is summary().
Step 2: Check the options for correct syntax
Only summary(df) uses the correct function summary() applied to the data frame df.
Final Answer:
summary(df) -> Option C
Quick Check:
summary() is the correct function [OK]
Hint: Use summary() for data frames, not summarize() or others [OK]
Common Mistakes:
Using summarize() which is from dplyr, not base R
Typing summary_stats() which doesn't exist
Using stats() which is unrelated
3. What will be the output of the following R code?
vec <- c(5, 10, 15, 20, 25)
summary(vec)
medium
A. Min. 5, 1st Qu. 10, Median 15, Mean 15, 3rd Qu. 20, Max. 25
B. Min. 5, Median 10, Mean 15, Max. 25
C. Min. 5, Max. 25, Sum 75
D. Error: summary() cannot be applied to vectors
Solution
Step 1: Calculate summary statistics for the vector
The vector is 5, 10, 15, 20, 25. Minimum is 5, 1st quartile is 10, median is 15, mean is (5+10+15+20+25)/5 = 15, 3rd quartile is 20, maximum is 25.
Step 2: Match calculated values with options
Min. 5, 1st Qu. 10, Median 15, Mean 15, 3rd Qu. 20, Max. 25 matches all these values exactly. Other options miss quartiles or give errors.
Final Answer:
Min. 5, 1st Qu. 10, Median 15, Mean 15, 3rd Qu. 20, Max. 25 -> Option A
Quick Check:
summary(vec) = min 5, Q1 10, median 15, mean 15, Q3 20, max 25 [OK]
Hint: Calculate min, quartiles, median, mean, max for vector [OK]
Common Mistakes:
Forgetting to calculate quartiles
Confusing median with mean
Expecting an error for summary() on vectors
4. Identify the error in this R code that tries to get summary statistics of a numeric vector nums:
summary(nums, digits=2)
medium
A. The vector nums must be sorted before summary()
B. The vector nums must be converted to a data frame first
C. The function should be summarize() instead of summary()
D. The summary() function does not have a digits argument
Solution
Step 1: Check the arguments of summary() in R
The base R summary() function does not accept a digits argument. This causes an error.
Step 2: Verify other options
Converting to data frame or sorting is not required. summarize() is from dplyr, not base R.
Final Answer:
The summary() function does not have a digits argument -> Option D
Quick Check:
summary() has no digits argument [OK]
Hint: Check function arguments carefully to avoid errors [OK]
Common Mistakes:
Assuming summary() accepts digits argument
Thinking vector must be data frame
Confusing summary() with dplyr's summarize()
5. You have a data frame df with a numeric column age. You want to find the median age but exclude any missing values (NA). Which R code correctly does this?
hard
A. median(df$age)
B. median(df$age, na.rm = TRUE)
C. summary(df$age, na.rm = TRUE)["Median"]
D. summary(df$age)$median
Solution
Step 1: Understand how to calculate median excluding NA values
The median() function has an argument na.rm = TRUE to remove missing values before calculation.
Step 2: Check each option for correctness
median(df$age, na.rm = TRUE) correctly uses median() with na.rm = TRUE. summary(df$age, na.rm = TRUE)["Median"] tries to extract median from summary but summary() does not accept na.rm. median(df$age) ignores NA values causing NA result. summary(df$age)$median tries to access median as a list element which is invalid.
Final Answer:
median(df$age, na.rm = TRUE) -> Option B
Quick Check:
Use median() with na.rm=TRUE to exclude NA [OK]
Hint: Use median(..., na.rm=TRUE) to ignore missing values [OK]
Common Mistakes:
Not using na.rm=TRUE causing NA result
Trying to get median from summary() output incorrectly
Assuming summary() removes NA automatically
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.