count_by: Count according to grouping

Description Usage Arguments Value Examples

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

1
2
3
4
5
6
7
8
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.

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

1
2
3
4
5
6
library(magrittr)
if (requireNamespace("survival", quietly = TRUE))
{
   survival::aml %>%
   count_by(x)
}

tidytidbits documentation built on March 8, 2021, 5:07 p.m.