tb: Convert Summarytools Objects into Tibbles

View source: R/tb.R

tbR Documentation

Convert Summarytools Objects into Tibbles

Description

Make a tidy dataset out of freq() or descr() outputs

Usage

tb(
  x,
  order = 1,
  drop.var.col = FALSE,
  recalculate = TRUE,
  fct.to.chr = FALSE,
  ...
)

Arguments

x

a freq() or descr() output object.

order

Integer. Useful for grouped results produced with stby or dplyr::group_by. When set to 1 (default), the ordering is done using the grouping variables first. When set to 2, the ordering is done according to the analytical (not grouping) variable. When set to 3, the same ordering as with 2 is used, but the analytical variable is placed in first position. Depending on what function was used for grouping, the results will be different in subtle ways. See Details.

drop.var.col

Logical. For descr objects, drop the variable column. This is possible only when statistics are produced for a single variable; when multiple variables are present, this parameter is ignored. FALSE by default.

recalculate

Logical. TRUE by default. For grouped freq results, recalculate percentages to have total proportions sum up to 1. Defaults to TRUE.

fct.to.chr

Logical. When grouped objects are created with dplyr::group_by, the resulting tibble will have factor columns when the grouping variable itself is a factor. To convert them to character, set this to TRUE. See Details.

...

For internal use only.

Details

stby, which is based on and by, initially make the first variable vary, keeping the other(s) constant. On the other hand, group_by initially keeps the first grouping variable(s) constant, making the last one vary. This will impact the ordering of the rows (and as a result, the cumulative percent columns, if present).

Also, keep in mind that while group_by shows NA groups by default, useNA = TRUE must be used to achieve the same results with stby.

Value

A tibble which is constructed following the tidy principles.

Examples


tb(freq(iris$Species))
tb(descr(iris, stats = "common"))

data("tobacco")
tb(stby(tobacco, tobacco$gender, descr, stats = "fivenum",check.nas = FALSE), 
   order=3)
tb(stby(tobacco, tobacco$gender, descr, stats = "common", useNA = TRUE))

# Compare stby() and group_by() groups' ordering
tb(with(tobacco, stby(diseased, list(gender, smoker), freq, useNA = TRUE)))

## Not run: 
tobacco |> dplyr::group_by(gender, smoker) |> freq(diseased) |> tb()

## End(Not run)


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