- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
> 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
> 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
> 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
> 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
- Related Articles
- How to find the row wise mode of strings in an R data frame?
- How to find the row-wise index of non-NA values in a matrix in R?
- How to find the row product of a matrix in R?
- How to find the variance of row elements of a matrix in R?
- JavaScript Program to Find median in row wise sorted matrix
- How to search in a row wise and column wise increased matrix using C#?
- Find median in row wise sorted matrix in C++
- Row-wise vs column-wise traversal of matrix in C++
- How to find the row-wise frequency of zeros in an R data frame?
- How to find the row wise sum for n number of columns in R?
- How to search in a row wise increased matrix using C#?
- How to find the row products for each row in an R matrix?
- Sort the matrix row-wise and column-wise using Python
- How to find the sum product of two matrix by row in R?
- How to convert the row values in a matrix to row percentage in R?

Advertisements