min, max, range
min, max, and range tell you the smallest, the largest, and the gap between them, like finding the shortest and tallest kids in a line. They give a quick sense of how far numbers stretch.
In this page:
Syntax
min(x)
max(x)
range(x)
min, max, range
min() and max() return the smallest and largest values in a vector, while range() returns both at once as a two-element vector.
These are simple but essential for quickly understanding a dataset's boundaries. All three work with numeric and character vectors alike (character comparison is lexicographic).
Note:
range(x) is shorthand for c(min(x), max(x)) computed together in one call.
Example: min, max, range
values <- c(23, 5, 67, 12, 45)
cat("Min:", min(values), "\n")
cat("Max:", max(values), "\n")
print(range(values))
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Using
range(values)and expecting the size of the range, when it returns the minimum and maximum, not their difference; usediff(range(values)). - Calling
max(a, b)on two vectors and expecting element-wise results, whenpmax()does that. - Using
max()on a character vector and being surprised it sorts alphabetically.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: