Get the position of the maximum element in each Row of a Matrix in R Programming – max.col() Function

max.col() function in R Language check for maximum value in each row and returns the column no. for it.

Syntax: max.col(x, ties.method)

Parameters:
x: Numeric matrix
ties.method: It takes random, first, and last as value and returns the position accordingly in case of a tie.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to find positions 
# of maximum elements of a matrix
  
# Creating matrices
m1 <- matrix(c(1:4), 2)
m2 <- matrix(c(4, 1, 2, 3), 2)
m3 <- matrix(c(1:9), 3, 3)
  
# Calling max.col() function
max.col(m1)
max.col(m2)
max.col(m3)

chevron_right


Output:

[1] 2 2
[1] 1 2
[1] 3 3 3

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to find positions 
# of maximum elements of a matrix
  
# Creating matrices
m1 <- matrix(c(2, 3, 2, 4), 2)
m2 <- matrix(c(2, 3, 2, 4), 2)
m3 <- matrix(c(2, 3, 2, 4), 2)
m1
  
# Calling max.col() function
max.col(m1, ties.method = "random")
max.col(m2, ties.method = "first")
max.col(m3, ties.method = "last")

chevron_right


Output:

     [, 1] [, 2]
[1, ]    2    2
[2, ]    3    4
[1] 2 2
[1] 1 2
[1] 2 2



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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.