count_by: Count according to grouping

View source: R/dplyr-like.R

count_byR Documentation

Count according to grouping

Description

Similar to dplyr::count(), but also adds the relative proportion and a percent-formatted string of the relative proportion, and allows to specify the column names.

Usage

count_by(
  .tbl,
  ...,
  column_names = c("n", "rel", "percent"),
  percentage_label_decimal_places = 1,
  add_grouping = T,
  na.rm = F
)

Arguments

.tbl

A data frame

...

Columns / expressions by which to group / which shall be used for counting.

column_names

vector if size 1 to 3, giving the names of (in order if unnamed, or named with n, rel, percent) the column containing the count, the relative proportion, and the latter formatted as a percent label. If a name is not contained, it will not be added (requires named vector).

percentage_label_decimal_places

Decimal precision of the percent label

add_grouping

Shall a pre-existing grouping be preserved for counting (adding the newly specified grouping)? Default is yes, which differs from group_by.

na.rm

Shall NA values be removed prior to counting?

Value

The counted data frame

Examples

library(magrittr)
if (requireNamespace("survival", quietly = TRUE))
{
   survival::aml %>%
   count_by(x)
}

tidytidbits documentation built on March 18, 2022, 6:10 p.m.