R filter data and count unique records

Suppose I have a dataframe like:

A    B    C    D  
1    1    1    1  
1    1    1    1  
2    2    1    2  
2    2    2    2  
2    2    1    2  

      

And I want to create a dataframe that only has unique entries and the number of times it has happened. So something like this:

A    B    C    D    count
1    1    1    1     2  
2    2    1    2     2   
2    2    2    2     1  

      

How should I do it?

+3


source to share


2 answers


You can try using the data.table package, for example:

> library(data.table)
> as.data.table(dat)[, .N, by = names(dat)]
   A B C D N
1: 1 1 1 1 2
2: 2 2 1 2 2
3: 2 2 2 2 1

      



Or similar to "dplyr":

> library(dplyr)
> dat %>% group_by_(.dots = names(dat)) %>% summarise(n = n())
Source: local data frame [3 x 5]
Groups: A, B, C

  A B C D n
1 1 1 1 1 2
2 2 2 1 2 2
3 2 2 2 2 1

      

+4


source


base R

Option A

aggregate(cbind(Count=1:nrow(df1))~., df1, FUN=length)
#    A B C D Count
#  1 1 1 1 1     2
#  2 2 2 1 2     2
#  3 2 2 2 2     1

      



Or a modification suggested by @David Arenburg

aggregate(Count ~ ., cbind(Count = 1, df1), FUN=length)

      

+3


source







All Articles