How to find the row-wise mode of a matrix in R?


There is no in-built function to find the mode in R, hence we need to create one and then apply it to the rows of the matrix. The function for mode is created as follows −

mode<-function(x){which.max(tabulate(x))}

Now consider we have a matrix called M then we can apply the above function as shown below −

apply(M2,1,mode)

Example1

Live Demo

> M1<-matrix(sample(1:2,25,replace=TRUE),ncol=5)
> M1

Output

     [,1] [,2] [,3] [,4] [,5]
[1,]  2    2    1    2   2
[2,]  2    2    2    2   1
[3,]  2    2    1    1   1
[4,]  2    1    1    1   1
[5,]  2    1    1    2   2
> apply(M1,1,mode)

Output

[1] 2 2 1 1 2

Example2

Live Demo

> M2<-matrix(sample(1:2,100,replace=TRUE),ncol=5)
> M2

Output

     [,1] [,2] [,3] [,4] [,5]
[1,]  1    1    2    2    1
[2,]  2    1    1    2    1
[3,]  2    2    1    1    1
[4,]  2    1    1    2    2
[5,]  2    1    1    2    2
[6,]  1    2    1    1    2
[7,]  1    1    2    1    2
[8,]  2    2    1    2    1
[9,]  2    1    1    2    2
[10,] 1    1    2    2    2
[11,] 1    1    2    1    2
[12,] 1    2    2    2    1
[13,] 2    2    2    2    1
[14,] 2    1    2    2    1
[15,] 1    2    1    1    2
[16,] 2    2    1    2    1
[17,] 2    2    1    1    1
[18,] 2    1    1    2    1
[19,] 1    1    1    2    1
[20,] 2    1    1    2    2
> apply(M2,1,mode)

Output

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

Example3

Live Demo

> M3<-matrix(sample(1:3,100,replace=TRUE),ncol=5)
> M3

Output

     [,1] [,2] [,3] [,4] [,5]
[1,]  1    3    3    2    1
[2,]  2    3    1    2    2
[3,]  2    2    3    3    1
[4,]  1    3    1    3    2
[5,]  3    1    2    1    2
[6,]  2    3    1    1    1
[7,]  2    2    2    3    1
[8,]  1    2    2    2    2
[9,]  2    1    2    1    2
[10,] 1    3    1    2    1
[11,] 2    1    3    1    1
[12,] 1    1    3    2    2
[13,] 2    1    1    1    2
[14,] 2    1    3    3    2
[15,] 1    2    3    1    2
[16,] 1    2    1    2    1
[17,] 3    1    1    3    2
[18,] 3    3    3    3    1
[19,] 3    2    3    1    1
[20,] 3    3    2    2    1
> apply(M3,1,mode)

Output

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

Example4

Live Demo

> M4<-matrix(sample(9:10,100,replace=TRUE),ncol=5)
> M4

Output

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


> apply(M4,1,mode)

Output

[1] 10 9 9 10 10 10 9 9 9 9 9 9 10 10 9 9 10 9 9 9

Updated on: 05-Mar-2021

422 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements