- 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 that has maximum number of duplicates in an R matrix?
To find the row that has maximum number of duplicates in an R matrix, we can follow the below steps −
- First of all, create a matrix.
- Then, convert matrix into data.table then use order function with head function to find the row that has maximum number of duplicates.
Create the matrix
Let’s create a matrix as shown below −
M<-matrix(rpois(40,1),ncol=2) M
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
[,1] [,2] [1,] 2 2 [2,] 0 1 [3,] 2 2 [4,] 1 0 [5,] 1 2 [6,] 3 0 [7,] 1 0 [8,] 0 0 [9,] 0 3 [10,] 0 1 [11,] 1 0 [12,] 1 0 [13,] 1 1 [14,] 0 1 [15,] 1 2 [16,] 0 0 [17,] 1 0 [18,] 1 1 [19,] 2 2 [20,] 1 2
Find the row that has maximum number of duplicates
Loading data.table package then converting matrix M into data.table object and finding the row that has maximum number of duplicates −
M<-matrix(rpois(40,1),ncol=2) library(data.table) DT<-data.table(M) head(DT[,list(Duplicates=.N),by=names(DT)][order(Duplicates,decreasing=T)],1)
Output
V1 V2 Duplicates 1: 1 0 5
- Related Articles
- How to find the row and column number for the minimum and maximum values in an R matrix?
- How to find the row products for each row in an R matrix?
- How to divide the matrix rows by row maximum in R?
- How to find the maximum of each row in an R data frame?
- How to find the row product of a matrix in R?
- How to find the row sum for each column by row name in an R matrix?
- Find the column number with largest value for each row in an R matrix.
- Find row number of a binary matrix having maximum number of 1s in C++
- How to find the number of columns where all row values are equal in R matrix?
- How to find the row means for each matrix stored in an R list?
- How to find the row-wise mode of a matrix in R?
- How to find the index of n number of maximums in each row of a matrix in R?
- How to find the variance of row elements of a matrix in R?
- How to find the maximum value in each matrix stored in an R list?
- How to find the sum product of two matrix by row in R?

Advertisements