stby: Obtain Grouped Statistics With summarytools

View source: R/stby.R

stbyR Documentation

Obtain Grouped Statistics With summarytools

Description

An adaptation base R's by function, designed to optimize the results' display.

Usage

stby(data, INDICES, FUN, ..., useNA = FALSE)

Arguments

data

an R object, normally a data frame, possibly a matrix.

INDICES

a grouping variable or a list of grouping variables, each of length nrow(data).

FUN

a function to be applied to (usually data-frame) subsets of data.

...

Further arguments to FUN.

useNA

Make NA a valid grouping value in INDICES variable(s). Set to FALSE explicitly to eliminate message.

Details

When the grouping variable(s) contain NA values, the base::by function (as well as summarytools versions prior to 1.1.0) ignores corresponding groups. Version 1.1.0 allows setting useNA = TRUE to make new groups using NA values on the grouping variable(s), just as dplyr::group_by does.

When NA values are detected and useNA = FALSE, a message is displayed; to disable this message, set check.nas = FALSE.

Value

An object of classes “list” and “summarytools”, giving results for each subset.

See Also

by, group_by

Examples

data("tobacco")
with(tobacco, stby(data = BMI, INDICES = gender, FUN = descr,
                   check.nas = FALSE))
with(tobacco, stby(data = smoker, INDICES = gender, freq, useNA = TRUE))
with(tobacco, stby(data = list(x = smoker, y = diseased),
                   INDICES = gender, FUN = ctable, useNA = TRUE))
                   

dcomtois/summarytools documentation built on March 1, 2025, 8:50 p.m.