📖 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, maximum, and total sales.
🎯 Goal: Build a simple R script that calculates descriptive statistics (mean, min, max, sum) 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, maximum, and total sales using built-in R functions
Print the results clearly
💡 Why This Matters
🌍 Real World
Descriptive statistics help summarize and understand data quickly in many fields like business, healthcare, and research.
💼 Career
Data analysts and scientists use these basic statistics daily to report insights and make decisions.
Progress0 / 4 steps
1
Create the sales data vector
Create a numeric vector called daily_sales with these exact values: 150, 200, 170, 220, 180, 210, 190.
R Programming
Hint
Use the c() function to create a vector with the given numbers.
2
Create a variable for the number of days
Create a variable called num_days that stores the length of the daily_sales vector using the length() function.
R Programming
Hint
Use length(daily_sales) to find how many days of sales data you have.
3
Calculate descriptive statistics
Calculate the mean, minimum, maximum, and total sales from daily_sales and store them in variables called avg_sales, min_sales, max_sales, and total_sales respectively. Use the functions mean(), min(), max(), and sum().
R Programming
Hint
Use the built-in R functions to calculate each statistic and assign them to the correct variable names.
4
Print the descriptive statistics
Print the values of avg_sales, min_sales, max_sales, and total_sales each on a new line with descriptive text using the print() function.
R Programming
Hint
Use print(paste("Text", variable)) to show descriptive text and the value on the same line.
Practice
(1/5)
1. Which of the following best describes the mean in descriptive statistics?
easy
A. The average value of a numeric dataset
B. The middle value when data is sorted
C. The most frequent value in the dataset
D. The difference between the largest and smallest values
Solution
Step 1: Understand the definition of mean
The mean is calculated by adding all numbers and dividing by the count.
Step 2: Compare with other options
Median is the middle value, mode is most frequent, range is difference between max and min.
Final Answer:
The average value of a numeric dataset -> Option A
Quick Check:
Mean = Average [OK]
Hint: Mean is sum divided by count, simple average [OK]
Common Mistakes:
Confusing mean with median
Thinking mean is the most frequent value
Mixing mean with range
2. Which R function gives a quick summary including min, 1st Qu., median, mean, 3rd Qu., and max for numeric data?
easy
A. mean()
B. var()
C. sd()
D. summary()
Solution
Step 1: Recall function purposes
mean() calculates average, var() variance, sd() standard deviation.
Step 2: Identify summary function
summary() provides min, quartiles, median, mean, and max in one output.
Final Answer:
summary() -> Option D
Quick Check:
summary() = quick overview [OK]
Hint: summary() shows min, quartiles, median, mean, max [OK]
Common Mistakes:
Using mean() expecting full summary
Confusing sd() with summary()
Trying var() for descriptive summary
3. What is the output of this R code?
data <- c(4, 8, 6, 5, 3, 7) mean(data)
medium
A. 5.5
B. 6
C. 4.5
D. 5
Solution
Step 1: Sum the data values
4 + 8 + 6 + 5 + 3 + 7 = 33
Step 2: Divide sum by number of values
There are 6 values, so 33 / 6 = 5.5
Final Answer:
5.5 -> Option A
Quick Check:
Mean = 33/6 = 5.5 [OK]
Hint: Add all numbers, divide by count [OK]
Common Mistakes:
Adding incorrectly
Dividing by wrong count
Confusing mean with median
4. Identify the error in this R code for calculating variance:
var() accepts na.rm=TRUE to remove missing values; syntax is correct.
Final Answer:
No error, code runs correctly -> Option B
Quick Check:
var() with na.rm=TRUE is valid [OK]
Hint: var() accepts na.rm=TRUE to ignore NAs [OK]
Common Mistakes:
Thinking na.rm is invalid for var()
Assuming missing parentheses error
Not defining data before use
5. You have a numeric vector v <- c(10, 20, 30, NA, 40, 50). Which R code correctly calculates the mean ignoring the missing value?
hard
A. mean(na.exclude(v))
B. mean(v, na.rm=TRUE)
C. Both B and C
D. mean(v)
Solution
Step 1: Understand missing value handling
By default, mean() returns NA if vector has NA values.
Step 2: Check options to ignore NA
mean(v, na.rm=TRUE) removes NA before calculation. mean(na.exclude(v)) also removes NA values before calculation, so it returns the mean ignoring NA as well. mean(v) returns NA.
Final Answer:
Both mean(v, na.rm=TRUE) and mean(na.exclude(v)) -> Option C
Quick Check:
na.rm=TRUE and na.exclude() both remove NA for mean calculation [OK]
Hint: Use na.rm=TRUE or na.exclude() to ignore NA [OK]
Common Mistakes:
Using mean() without na.rm=TRUE returns NA
Not removing NA before mean calculation
Confusing na.rm with na.exclude
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.