Returns row counts of the dataset.
tally()
returns counts by group on a grouped tidytable.
count()
returns counts by group on a grouped tidytable, or column names can be specified
to return counts by group.
count(.df, ..., wt = NULL, sort = FALSE, name = NULL)
tally(.df, wt = NULL, sort = FALSE, name = NULL)
A data.frame or data.table
Columns to group by in count()
. tidyselect
compatible.
Frequency weights. tidyselect
compatible.
Can be NULL
or a variable:
If NULL
(the default), counts the number of rows in each group.
If a variable, computes sum(wt)
for each group.
If TRUE
, will show the largest groups at the top.
The name of the new column in the output.
If omitted, it will default to n
.
df <- data.table(
x = c("a", "a", "b"),
y = c("a", "a", "b"),
z = 1:3
)
df %>%
count()
#> # A tidytable: 1 × 1
#> n
#> <int>
#> 1 3
df %>%
count(x)
#> # A tidytable: 2 × 2
#> x n
#> <chr> <int>
#> 1 a 2
#> 2 b 1
df %>%
count(where(is.character))
#> # A tidytable: 2 × 3
#> x y n
#> <chr> <chr> <int>
#> 1 a a 2
#> 2 b b 1
df %>%
count(x, wt = z, name = "x_sum")
#> # A tidytable: 2 × 2
#> x x_sum
#> <chr> <int>
#> 1 a 3
#> 2 b 3
df %>%
count(x, sort = TRUE)
#> # A tidytable: 2 × 2
#> x n
#> <chr> <int>
#> 1 a 2
#> 2 b 1
df %>%
tally()
#> # A tidytable: 1 × 1
#> n
#> <int>
#> 1 3
df %>%
group_by(x) %>%
tally()
#> # A tidytable: 2 × 2
#> x n
#> <chr> <int>
#> 1 a 2
#> 2 b 1