'count by all variables / count distinct with 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 ?



Solution 1:[1]

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)

Solution 2:[2]

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

Solution 3:[3]

Have you considered the (now superceded) group_by_all()?

df1 <- data.frame(x=rep(letters[1:3],1:3),y=rep(letters[1:3],1:3))
df1 %>% group_by_all() %>% count
df1 %>% group_by(across()) %>% count()
df1 %>% count(across()) # don't know why this returns a data.frame and not tibble

See the colwise vignette "other verbs" section for explanation... though honestly I get turned around myself sometimes.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Vongo
Solution 3 Mike Dolan Fliss