Summary statistics in R Programming - Time & Space Complexity
We want to know how the time to calculate summary statistics changes as the data grows.
How does the number of operations grow when we summarize more data?
Analyze the time complexity of the following code snippet.
data <- c(4, 8, 15, 16, 23, 42)
mean_val <- mean(data)
median_val <- median(data)
min_val <- min(data)
max_val <- max(data)
sd_val <- sd(data)
This code calculates basic summary statistics like mean, median, min, max, and standard deviation for a numeric vector.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Each summary function scans through the entire data vector once.
- How many times: There are 5 separate scans, one for each statistic.
Each function looks at every number once, so the work grows directly with data size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 50 (5 times 10) |
| 100 | About 500 (5 times 100) |
| 1000 | About 5000 (5 times 1000) |
Pattern observation: The total work grows linearly as the data size grows.
Time Complexity: O(n)
This means the time to compute summary statistics grows in direct proportion to the number of data points.
[X] Wrong: "Calculating all summary statistics takes constant time regardless of data size."
[OK] Correct: Each statistic needs to look at every data point, so more data means more work.
Understanding how summary statistics scale helps you explain data processing speed clearly and confidently in real projects.
"What if we calculate summary statistics only once but for multiple columns in a data frame? How would the time complexity change?"
Practice
summary() function in R provide when applied to a numeric vector?Solution
Step 1: Understand what
Thesummary()does for numeric vectorssummary()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 AQuick Check:
summary() = min, Q1, median, mean, Q3, max [OK]
- Thinking summary() only gives mean and median
- Confusing summary() with sum() or length()
- Expecting summary() to count unique values
df in R?Solution
Step 1: Recall the correct function name for summary statistics in R
The built-in function to get summary statistics issummary().Step 2: Check the options for correct syntax
Only summary(df) uses the correct functionsummary()applied to the data framedf.Final Answer:
summary(df) -> Option CQuick Check:
summary() is the correct function [OK]
- Using summarize() which is from dplyr, not base R
- Typing summary_stats() which doesn't exist
- Using stats() which is unrelated
vec <- c(5, 10, 15, 20, 25) summary(vec)
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 AQuick Check:
summary(vec) = min 5, Q1 10, median 15, mean 15, Q3 20, max 25 [OK]
- Forgetting to calculate quartiles
- Confusing median with mean
- Expecting an error for summary() on vectors
nums:summary(nums, digits=2)
Solution
Step 1: Check the arguments of summary() in R
The base Rsummary()function does not accept adigitsargument. 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:
Thesummary()function does not have adigitsargument -> Option DQuick Check:
summary() has no digits argument [OK]
- Assuming summary() accepts digits argument
- Thinking vector must be data frame
- Confusing summary() with dplyr's summarize()
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?Solution
Step 1: Understand how to calculate median excluding NA values
Themedian()function has an argumentna.rm = TRUEto remove missing values before calculation.Step 2: Check each option for correctness
median(df$age, na.rm = TRUE) correctly usesmedian()withna.rm = TRUE. summary(df$age, na.rm = TRUE)["Median"] tries to extract median from summary but summary() does not acceptna.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 BQuick Check:
Use median() with na.rm=TRUE to exclude NA [OK]
- Not using na.rm=TRUE causing NA result
- Trying to get median from summary() output incorrectly
- Assuming summary() removes NA automatically
Ready to prove it?
Pass the test and this pattern moves unknown
