Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count by all variables / count distinct with dplyr

Tags:

r

dplyr

Say I have this data.frame :

library(dplyr)
df1 <- data.frame(x=rep(letters[1:3],1:3),y=rep(letters[1:3],1:3))
#   x y
# 1 a a
# 2 b b
# 3 b b
# 4 c c
# 5 c c
# 6 c c

I can group and count easily by mentioning the names :

df1 %>%
  count(x,y)
# A tibble: 3 x 3
#        x      y     n
#   <fctr> <fctr> <int>
# 1      a      a     1
# 2      b      b     2
# 3      c      c     3

How do I do to group by everything without mentioning individual column names, in the most compact /readable way ?

like image 605
Moody_Mudskipper Avatar asked Mar 05 '23 22:03

Moody_Mudskipper


2 Answers

We can pass the input itself to the ... argument and splice it with !!! :

df1 %>% count(., !!!.) 
#>   x y n
#> 1 a a 1
#> 2 b b 2
#> 3 c c 3

Note : see edit history to make sense of some comments

With base we could do : aggregate(setNames(df1[1],"n"), df1, length)

like image 83
Moody_Mudskipper Avatar answered Mar 21 '23 06:03

Moody_Mudskipper


For those who wouldn't get the voodoo you are using in the accepted answer, if you don't need to use dplyr, you can do it with data.table:

setDT(df1)
df1[, .N, names(df1)]
#    x y N
# 1: a a 1
# 2: b b 2
# 3: c c 3

like image 23
Vongo Avatar answered Mar 21 '23 04:03

Vongo