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)
.
add_count()
and add_tally()
are equivalents to count()
and tally()
but use mutate()
instead of summarise()
so that they add a new column
with group-wise counts.
# S3 method for class 'SummarizedExperiment'
count(
x,
...,
wt = NULL,
sort = FALSE,
name = NULL,
.drop = group_by_drop_default(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.
<data-masking
> Frequency weights.
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
. If there's already a column called n
,
it will use nn
. If there's a column called n
and nn
, it'll use
nnn
, and so on, adding n
s until it gets a new name.
Handling of factor levels that don't appear in the data, passed
on to group_by()
.
For count()
: if FALSE
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.
An object of the same type as .data
. count()
and add_count()
group transiently, so the output has the same groups as the input.
Hutchison, W.J., Keyes, T.J., The tidyomics Consortium. et al. The tidyomics ecosystem: enhancing omic data analyses. Nat Methods 21, 1166–1170 (2024). https://doi.org/10.1038/s41592-024-02299-2
Wickham, H., François, R., Henry, L., Müller, K., Vaughan, D. (2023). dplyr: A Grammar of Data Manipulation. R package version 2.1.4, https://CRAN.R-project.org/package=dplyr
data(se)
se |> count(dex)
#> tidySummarizedExperiment says: A data frame is returned for independent data analysis.
#> # A tibble: 2 × 2
#> dex n
#> <fct> <int>
#> 1 trt 400
#> 2 untrt 400