Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to count the number of duplicate rows in an R data frame?
To count the number of duplicate rows in an R data frame, we would first need to convert the data frame into a data.table object by using setDT and then count the duplicates with Count function. For example, if we have a data frame called df then the duplicate rows will be counted by using the command − setDT(df)[,list(Count=.N),names(df)].
Example1
y1<−sample(0:2,20,replace=TRUE) y2<−sample(0:2,20,replace=TRUE) df2<−data.frame(y1,y2) df2
Output
y1 y2 1 2 1 2 2 2 3 0 0 4 2 2 5 0 2 6 2 2 7 1 0 8 0 2 9 1 0 10 2 1 11 1 2 12 0 2 13 1 0 14 0 0 15 2 1 16 1 1 17 0 0 18 0 1 19 2 1 20 2 0
Finding the duplicate rows −
Example
setDT(df2)[,list(Count=.N),names(df2)]
Output
y1 y2 Count 1: 2 1 4 2: 2 2 3 3: 0 0 3 4: 0 2 3 5: 1 0 3 6: 1 2 1 7: 1 1 1 8: 0 1 1 9: 2 0 1
Advertisements
