R – Max, Min, Mean, Median, Mode (with Code Explanation)
Introduction – Core Descriptive Statistics in R
When analyzing data in R, it’s essential to start with the five most basic statistics:
- Maximum, Minimum: Identify extreme values
- Mean, Median: Measure central tendency
- Mode: Identify most frequent value
R provides simple functions for all of these—making it easy to summarize and explore your datasets efficiently.
In this guide, you’ll learn:
- How to calculate min, max, mean, median, and mode in R
- How to use them on numeric vectors or columns in data frames
- Understand when each metric is useful
1. Minimum and Maximum in R
x <- c(12, 7, 19, 4, 11)
min(x) # Output: 4
max(x) # Output: 19
Explanation:
min(x): Returns the smallest value in the vectormax(x): Returns the largest value in the vector
Use it on data frames:
min(mtcars$mpg)
max(mtcars$mpg)
2. Mean (Average)
mean(x) # Output: 10.6
Explanation:
- Calculates arithmetic average
- Useful when data has no major outliers
3. Median
median(x) # Output: 11
Explanation:
- Finds the middle value after sorting
- Robust to outliers, ideal for skewed data
4. Mode in R (Custom Function)
R does not have a built-in mode() function for numeric vectors. Here’s how you define one:
get_mode <- function(v) {
uniq_vals <- unique(v)
uniq_vals[which.max(tabulate(match(v, uniq_vals)))]
}
get_mode(x) # Output: most frequent value
Explanation:
unique()extracts distinct valuestabulate()counts how often each appearswhich.max()identifies the highest frequency
5. Summary on Real Dataset (mtcars)
data(mtcars)
mean(mtcars$hp) # Mean horsepower
median(mtcars$hp) # Median horsepower
min(mtcars$hp) # Min horsepower
max(mtcars$hp) # Max horsepower
get_mode(mtcars$cyl) # Most common cylinder count
6. Compare All Stats Together
x <- mtcars$mpg
cat("Min:", min(x), "\n")
cat("Max:", max(x), "\n")
cat("Mean:", mean(x), "\n")
cat("Median:", median(x), "\n")
cat("Mode:", get_mode(x), "\n")
Sample Output:
Min: 10.4
Max: 33.9
Mean: 20.1
Median: 19.2
Mode: 21
Summary – Recap & Next Steps
These five statistics form the backbone of data profiling. Use them as a first step in any data analysis to understand range, center, and repetition in your dataset.
Key Takeaways:
min()andmax()give the rangemean()is the average, sensitive to outliersmedian()is more robust to skewed valuesmode()requires a custom function but shows repetition- Combine all to form a complete summary
Real-World Relevance:
Used in data cleaning, quality checks, summary reports, dashboards, and statistical modeling.
FAQs – Descriptive Stats in R
Why doesn’t R have a built-in mode function?
R supports categorical modes, but numeric vectors require a custom function using match() and tabulate().
What happens if there are multiple modes?
The function shown returns the first most frequent value. You can enhance it to return all tied modes.
What is the difference between mean and median?
Mean is the average, affected by outliers. Median is the middle value—great for skewed distributions.
How to apply these functions to multiple columns?
Use sapply():
sapply(mtcars[, c("mpg", "hp")], mean)
Can I visualize these statistics?
Yes. Use:
boxplot(mtcars$mpg)
hist(mtcars$mpg)
Share Now :
