These are methods for the dplyr count()
and tally()
generics. They
wrap up group_by.tbl_lazy()
, summarise.tbl_lazy()
and, optionally,
arrange.tbl_lazy()
.
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, or functions of variables. Usedesc()
to sort a variable in descending order.- 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
Not supported for lazy tables.
Examples
library(dplyr, warn.conflicts = FALSE)
db <- memdb_frame(g = c(1, 1, 1, 2, 2), x = c(4, 3, 6, 9, 2))
db %>% count(g) %>% show_query()
#> <SQL>
#> SELECT `g`, COUNT(*) AS `n`
#> FROM `dbplyr_jRmVMuJacg`
#> GROUP BY `g`
db %>% count(g, wt = x) %>% show_query()
#> <SQL>
#> SELECT `g`, SUM(`x`) AS `n`
#> FROM `dbplyr_jRmVMuJacg`
#> GROUP BY `g`
db %>% count(g, wt = x, sort = TRUE) %>% show_query()
#> <SQL>
#> SELECT `g`, SUM(`x`) AS `n`
#> FROM `dbplyr_jRmVMuJacg`
#> GROUP BY `g`
#> ORDER BY `n` DESC