range()
range(..., na.rm = FALSE) Returns:
numeric · Updated March 13, 2026 · Base Functions statistics range base min max
The range() function returns a numeric vector of length two containing the minimum and maximum values of the input. It works on numeric vectors, integers, and character vectors (using alphabetical ordering).
Syntax
range(x, na.rm = FALSE, ...)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | numeric, integer, or character | — | A vector for which to find the min and max |
na.rm | logical | FALSE | If TRUE, remove NA values before computing |
... | additional arguments | — | Optional arguments passed to min() or max() |
Examples
Basic usage
# Numeric vector
x <- c(3, 1, 4, 1, 5, 9, 2, 6)
range(x)
# [1] 1 9
With NA values
# Without removing NAs (default)
y <- c(1, 2, NA, 4, 5)
range(y)
# [1] NA NA
# With na.rm = TRUE
range(y, na.rm = TRUE)
# [1] 1 5
Character vectors
# Strings are sorted alphabetically
chars <- c("banana", "apple", "cherry", "date")
range(chars)
# [1] "apple" "date"
Common Patterns
Checking if values fall within a range
x <- c(1, 5, 10, 15, 20)
r <- range(x)
x >= r[1] & x <= r[2]
# [1] TRUE TRUE TRUE TRUE TRUE
Scaling data using range
# Normalize to 0-1 scale
x <- c(10, 20, 30, 40, 50)
r <- range(x)
(x - r[1]) / (r[2] - r[1])
# [1] 0.00 0.25 0.50 0.75 1.00