count_at: Count by multiple variables

View source: R/dplyr-like.R

count_atR Documentation

Count by multiple variables

Description

Count by multiple variables

Usage

count_at(
  .tbl,
  .vars,
  .grouping = vars(),
  label_style = "long",
  long_label_column_names = c("variable", "category"),
  column_names = c("n", "rel", "percent"),
  na_label = "missing",
  percentage_label_decimal_places = 1,
  add_grouping = T,
  na.rm = F
)

Arguments

.tbl

A data frame

.vars

A list of variables (created using vars()) for which count_by is to be called

.grouping

Additional grouping to apply prior to counting

label_style

Character vector containing one of "wide" and "long" or both.

  • "wide": Include labels in wide format, i.e., for each variable one column named as variable and giving the label for the corresponding count, but NA for all rows from different variables

  • "long": Include two meta columns, one giving the variable that is counted (value from .vars), the second giving the label (which value/category of the variable is counted?).

long_label_column_names

Character vector of size 2: If label_style contains "long", the names for the additional meta columns for variable and category

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).

na_label

If na.rm=F, label to use for counting NA values

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

A data frame concatenated from individual count_by results, with labels as per label_style.

Examples

library(magrittr)
library(datasets)
library(dplyr)
mtcars %>% count_at(vars(gear, cyl))

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