markfairbanks / tidytable

Tidy interface to 'data.table'
https://markfairbanks.github.io/tidytable/
Other
449 stars 33 forks source link

Implement `tally()` and `add_tally()` #608

Closed markfairbanks closed 2 years ago

markfairbanks commented 2 years ago

Note: tally() treats groups like summarize(.groups = "drop_last") and not like count() (which either uses .groups = "drop" or .groups = "keep" depending on how they're passed).

pacman::p_load(dplyr)

df <- tibble(x = c("a", "a", "b"), y = c("a", "a", "b"))

df %>%
  group_by(x, y) %>%
  tally()
#> # A tibble: 2 × 3
#> # Groups:   x [2]
#>   x     y         n
#>   <chr> <chr> <int>
#> 1 a     a         2
#> 2 b     b         1

df %>%
  group_by(x, y) %>%
  count()
#> # A tibble: 2 × 3
#> # Groups:   x, y [2]
#>   x     y         n
#>   <chr> <chr> <int>
#> 1 a     a         2
#> 2 b     b         1