Summary statistics help us quickly understand the main features of data by showing simple numbers like average, minimum, and maximum.
Summary statistics in R Programming
summary(data)
data can be a vector, data frame, or other R object.
The function returns minimum, 1st quartile, median, mean, 3rd quartile, and maximum for numeric data.
x <- c(5, 10, 15, 20, 25) summary(x)
df <- data.frame(age = c(21, 25, 30, 22, 28), score = c(80, 90, 85, 88, 92)) summary(df)
summary(factor(c('red', 'blue', 'red', 'green')))
This program creates two numeric vectors for ages and scores, puts them in a data frame, and prints summary statistics for each.
ages <- c(23, 29, 31, 22, 27, 35, 28) scores <- c(88, 92, 85, 90, 87, 95, 91) data <- data.frame(ages, scores) print("Summary of ages:") print(summary(data$ages)) print("Summary of scores:") print(summary(data$scores))
Summary statistics give a quick look but do not replace detailed analysis.
For large datasets, summary() is fast and helpful to spot data issues.
Non-numeric data like factors show counts of each category instead of numbers.
Summary statistics show key numbers like min, max, mean, and median.
Use summary() in R to get these numbers easily for vectors or data frames.
They help understand data quickly and find unusual values.
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
