This is a method for the dplyr::count()
generic.
See "Fallbacks" section for differences in implementation.
count()
lets you quickly count the unique values of one or more variables:
df %>% count(a, b)
is roughly equivalent to
df %>% group_by(a, b) %>% summarise(n = n())
.
count()
is paired with tally()
, a lower-level helper that is equivalent
to df %>% summarise(n = n())
. Supply wt
to perform weighted counts,
switching the summary from n = n()
to n = sum(wt)
.
Usage
# S3 method for class 'duckplyr_df'
count(
x,
...,
wt = NULL,
sort = FALSE,
name = NULL,
.drop = group_by_drop_default(x)
)
Arguments
- x
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr).
- ...
<
data-masking
> Variables to group by.- wt
<
data-masking
> Frequency weights. Can beNULL
or a variable:If
NULL
(the default), counts the number of rows in each group.If a variable, computes
sum(wt)
for each group.
- sort
If
TRUE
, will show the largest groups at the top.- name
The name of the new column in the output.
If omitted, it will default to
n
. If there's already a column calledn
, it will usenn
. If there's a column calledn
andnn
, it'll usennn
, and so on, addingn
s until it gets a new name.- .drop
Handling of factor levels that don't appear in the data, passed on to
group_by()
.For
count()
: ifFALSE
will include counts for empty groups (i.e. for levels of factors that don't exist in the data).For
add_count()
: deprecated since it can't actually affect the output.
Fallbacks
There is no DuckDB translation in count.duckplyr_df()
with complex expressions in
...
,with
.drop = FALSE
,with
sort = TRUE
.
These features fall back to dplyr::count()
, see vignette("fallback")
for details.