Get the Maximum element of an Object in R Programming – max() Function

max() function in R Language is used to find the maximum element present in an object. This object can be a Vector, a list, a matrix, a data frame, etc..

Syntax: max(object, na.rm)

Parameters:
object: Vector, matrix, list, data frame, etc.
na.rm: Boolean value to remove NA element.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate 
# the use of max() function
  
# Creating vectors
x1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
x2 <- c(1, 4, 2, 8, NA, 11)
  
# Finding Maximum element
max(x1)
max(x2, na.rm = FALSE)
max(x2, na.rm = TRUE)

chevron_right


Output:

[1] 9
[1] NA
[1] 11

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate 
# the use of max() function
  
# Creating a matrix
arr = array(2:13, dim = c(2, 3, 2)) 
print(arr)
  
# Using max() function
max(arr)

chevron_right


Output:

,, 1

     [, 1] [, 2] [, 3]
[1, ]    2    4    6
[2, ]    3    5    7,, 2

     [, 1] [, 2] [, 3]
[1, ]    8   10   12
[2, ]    9   11   13

[1] 13
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.