← Back to R Course | Chapter 9: Statistics | Lesson 2 of 7

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:

  1. min, max, range
Syntax
r
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

markup
values <- c(23, 5, 67, 12, 45)

cat("Min:", min(values), "\n")
cat("Max:", max(values), "\n")
print(range(values))
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
  1. Using range(values) and expecting the size of the range, when it returns the minimum and maximum, not their difference; use diff(range(values)).
  2. Calling max(a, b) on two vectors and expecting element-wise results, when pmax() does that.
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.