count {tidytable}R Documentation

Count observations by group

Description

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.

Usage

count(.df, ..., wt = NULL, sort = FALSE, name = NULL)

tally(.df, wt = NULL, sort = FALSE, name = NULL)

Arguments

.df

A data.frame or data.table

...

Columns to group by in count(). tidyselect compatible.

wt

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.

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.

Examples

df <- data.table(
  x = c("a", "a", "b"),
  y = c("a", "a", "b"),
  z = 1:3
)

df %>%
  count()

df %>%
  count(x)

df %>%
  count(where(is.character))

df %>%
  count(x, wt = z, name = "x_sum")

df %>%
  count(x, sort = TRUE)

df %>%
  tally()

df %>%
  group_by(x) %>%
  tally()

[Package tidytable version 0.11.1 Index]