rguides

max()

max(..., na.rm = FALSE, na.last = TRUE, type = c("ordinary", "ordered"))

max() returns the maximum value of its arguments. It is among the most frequently used functions for exploratory data analysis and statistical summaries.

Syntax

max(..., na.rm = FALSE, na.last = TRUE, type = c("ordinary", "ordered"))

Parameters

ParameterTypeDefaultDescription
...numericOne or more vectors, or atomic values. Multiple arguments are combined element-wise.
na.rmlogicalFALSEIf TRUE, NA values are removed before computing the result.
na.lastlogicalTRUEFor na.rm = FALSE, controls where NA is placed in the output.
typecharacter"ordinary"Either "ordinary" for standard comparison, or "ordered" for ordered factors.

Examples

Basic usage

x <- c(3, 1, 4, 1, 5, 9, 2, 6)
max(x)
# [1] 9

Multiple arguments

max(1, 2, 3, 4, 5)
# [1] 5

max(c(1, 2), c(3, 4), 5)
# [1] 5

Handling missing values

y <- c(1, 2, NA, 4, 5)

max(y)
# [1] NA

max(y, na.rm = TRUE)
# [1] 5

With different data types

# Character vectors (uses alphabetical order)
chars <- c("apple", "banana", "cherry")
max(chars)
# [1] "cherry"

# Logical vectors (TRUE = 1, FALSE = 0)
max(c(TRUE, FALSE, TRUE, FALSE))
# [1] 1

Common Patterns

Finding the range of a vector

data <- c(3, 1, 4, 1, 5, 9, 2, 6)
range_val <- range(data)
range_val
# [1] 1 9

max(data) - min(data)
# [1] 8

Identifying which element is max

scores <- c(85, 92, 78, 91, 88)
which.max(scores)
# [1] 2

# Get the actual value
scores[which.max(scores)]
# [1] 92

Normalizing by scaling to 0-1

values <- c(10, 20, 30, 40, 50)

scaled <- (values - min(values)) / (max(values) - min(values))
scaled
# [1] 0.00 0.25 0.50 0.75 1.00

See Also