Find the column index of least value for each row of an R matrix

R ProgrammingServer Side ProgrammingProgramming

To find the column index of least 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 least value for each row by using the command as follows −

apply(M,1,which.min)

Example 1

Consider the matrix given below −

M1<-matrix(round(rexp(80),1),ncol=4)
M1

The following dataframe is created

     [,1] [,2]  [,3] [,4]
[1,]  1.3  0.1  0.6  1.0
[2,]  0.7  2.5  0.0  0.5
[3,]  0.9  0.7  0.9  0.8
[4,]  0.5  1.0  0.6  0.5
[5,]  0.3  1.2  1.6  0.4
[6,]  0.7  0.5  0.1  1.9
[7,]  1.4  0.1  0.1  0.2
[8,]  2.6  1.4  0.1  1.1
[9,]  0.1  2.9  0.6  0.5
[10,] 1.9  1.6  1.0  1.6
[11,] 2.6  0.7  0.3  3.5
[12,] 0.4  1.4  1.0  1.9
[13,] 0.8  0.5  0.8  4.3
[14,] 0.2  1.7  0.8  0.4
[15,] 0.1  0.9  0.3  1.0
[16,] 2.2  1.9  0.3  2.2
[17,] 0.2  0.4  1.9  0.6
[18,] 0.8  1.2  3.4  0.4
[19,] 0.7  1.4  2.0  0.2
[20,] 0.7  0.2  1.0  2.5

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

M1<-matrix(round(rexp(80),1),ncol=4)
apply(M1,1,which.min)

Output

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

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

Example 2

Consider the matrix given below −

M2<-matrix(round(rnorm(80),2),ncol=4)
M2

The following dataframe is created

       [,1]  [,2]  [,3]  [,4]
[1,]   0.35 -0.41 -0.97  0.29
[2,]   0.88 -0.36 -0.13  0.23
[3,]   0.44 -0.26 -0.83  0.57
[4,]   1.46 -1.78  0.89 -0.07
[5,]  -0.02 -0.98  0.75  1.32
[6,]   0.69 -1.08  0.75  0.84
[7,]  -1.67 -1.16 -0.49  0.60
[8,]  -0.98 -0.61 -1.12  0.97
[9,]  -0.53  0.00  0.40 -1.01
[10,] -0.15  0.01  1.64  0.94
[11,] -0.01  0.50  0.18 -1.96
[12,]  0.01  0.95 -0.40 -1.06
[13,] -1.20  0.90 -0.83  0.88
[14,] -0.09 -1.44 -0.72  0.39
[15,] -0.41  0.87  0.27  0.57
[16,] -1.15 -1.31  0.76 -0.76
[17,] -0.42  0.88 -1.61  0.58
[18,] -0.99  1.21  0.05  0.25
[19,] -0.68  1.15  0.79  0.23
[20,] -0.44  0.64  0.16  0.54

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

M2<-matrix(round(rnorm(80),2),ncol=4)
apply(M2,1,which.min)

Output

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

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

Example 3

Consider the matrix given below −

M3<-matrix(rpois(100,10),ncol=5)
M3

The following dataframe is created

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

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

Consider the matrix given below −

M3<-matrix(rpois(100,10),ncol=5)
apply(M3,1,which.min)

Output

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

[1] 4 3 1 1 4 4 3 4 2 5 3 1 1 3 5 2 1 1 3 3
raja
Published on 01-Nov-2021 07:00:30
Advertisements