Find the column number with largest value for each row in an R matrix.

To check which column has the largest value for each row in an R matrix, we can use apply function.

For Example, if we have a matrix called M then we can find column that has the largest value for each row by using the command given below −

apply(M,1,which.max)

Example 1

Consider the matrix given below −

M1<-matrix(rpois(80,10),ncol=4)
M1


The following dataframe is created

     [,1] [,2] [,3] [,4]
[1,]   7    9   14    8
[2,]  13   11   14   11
[3,]  10   17   15   18
[4,]  12    4   11    9
[5,]  15    9   15   10
[6,]   8   13    6   16
[7,]  12    8   11   10
[8,]  11    9   12   17
[9,]  12    7    6    3
[10,]  8   11   12    9
[11,]  6    9    9    8
[12,] 15   10    6    6
[13,]  8    4    8   12
[14,] 13    9   11    6
[15,] 12   11    8    9
[16,] 17    8    6   18
[17,]  6   11    8    7
[18,] 11   13   10    8
[19,]  9 11 7 17
[20,]  8 13 13 17

To find the column number that has largest value for each row in M1 on the above created data frame, add the following code to the above snippet −

M1<-matrix(rpois(80,10),ncol=4)
apply(M1,1,which.max)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

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


Example 2

Consider the matrix given below −

M2<-matrix(round(rnorm(60),2),ncol=3)
M2

The following dataframe is created

       [,1]  [,2]  [,3]
[1,]   0.88 -1.37  0.95
[2,]  -1.05  1.53 -0.84
[3,]  -1.17 -0.35 -0.05
[4,]  -0.54 -0.54 -2.06
[5,]  -0.91 -1.02 -0.09
[6,]  -0.24 -0.05 -0.14
[7,]   0.13  0.01  0.99
[8,]   0.31 -0.36  0.34
[9,]  -0.31  1.34 -1.62
[10,]  0.22  1.75  1.15
[11,]  0.21 -0.77 -1.02
[12,] -2.85  1.91  0.30
[13,] -0.67 -0.17 -1.01
[14,] -1.58 -0.11 -1.36
[15,]  0.33  1.28 -0.04
[16,] -0.63  0.26  1.53
[17,]  0.39 -0.14  0.23
[18,]  1.10 -1.43  0.46
[19,]  1.56 -0.46 -1.09
[20,] -0.15 0.83   0.48

To find the column number that has largest value for each row in M2 on the above created data frame, add the following code to the above snippet −

M2<-matrix(round(rnorm(60),2),ncol=3)
apply(M2,1,which.max)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

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


Example 3

Consider the matrix given below −

M3<-matrix(round(rexp(60,1),1),ncol=3)
M3

The following dataframe is created

      [,1] [,2] [,3]
[1,]  0.8  0.7  0.1
[2,]  0.2  0.4  0.4
[3,]  0.9  0.0  0.3
[4,]  0.8  0.4  0.2
[5,]  2.5  4.2  1.1
[6,]  3.6  0.9  0.0
[7,]  0.2  0.0  1.1
[8,]  1.4  0.1  0.7
[9,]  0.5  2.0  0.3
[10,] 1.6  0.0  0.3
[11,] 0.6  0.0  2.3
[12,] 1.3  0.0  0.9
[13,] 1.1  0.7  1.5
[14,] 0.5  0.2  2.3
[15,] 0.5  0.7  0.5
[16,] 0.0  1.1  2.3
[17,] 0.2  0.5  0.1
[18,] 0.8  0.1  1.5
[19,] 2.2  0.6  0.1
[20,] 1.1  0.6  0.7

To find the column number that has largest value for each row in M3 on the above created data frame, add the following code to the above snippet −

M3<-matrix(round(rexp(60,1),1),ncol=3)
apply(M3,1,which.max)

If you execute all the above given snippets as a single program, it generates the following Output −

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


Updated on: 01-Nov-2021

338 Views