R max()

R max() function is used to find the maximum value among given arguments.

In this tutorial, we will learn how to use max() function to compute the maximum of given values.

Syntax

The syntax of max() function is

max(..., na.rm = FALSE)

where

  • ... are the values of which we have to find the maximum.
  • na.rm [optional] is a logical value indicating whether missing values should be removed.
ADVERTISEMENT

Examples

In the following program, we will find the maximum of three numbers.

example.R

x <- 16
y <- 25
z <- 9
result <- max(x, y, z)
cat("Maximum value is :", result)

Output

Maximum value is : 25

Now, let us take an array of numbers, and find the maximum of this array.

example.R

x <- array(c(1, 6, 19), dim=c(3,1))
result <- max(x)
cat("Maximum value is :", result)

Output

Maximum value is : 19

If no argument is provided to max() function, then it returns negative Infinity, along with a warning message.

example.R

result <- max()
cat("Maximum value is :", result)

Output

Warning message:
In max() : no non-missing arguments to max; returning -Inf
Maximum value is : -Inf

Conclusion

In this R Tutorial, we learned how to find the maximum value of given arguments using max() function, with the help of examples.