Learn R Programming

dtlg (version 0.0.2)

calc_stats: Calculate summary statistics for a variable

Description

calc_stats() calculates summary statistics for a variable on groups. This is a generic function; note that it dispatches based on the class of target (second argument), not dt (first argument).

Usage

calc_stats(
  dt,
  target,
  target_name = target,
  treat,
  indent = nbsp(n = 4L),
  .total_dt = NULL,
  pct_dec = 1
)

Value

A data.table of summary statistics. The format depends on the type of the target variable:

  • If the target variable is categorical, i.e. type character, factor or logical then the output is that of calc_counts().

  • If the target variable is numeric, then the output is that of calc_desc().

Arguments

dt

A data.frame containing, at least, the variables indicated in target and treat.

target

Target variable passed as a string for which summary statistics are to be calculated.

target_name

Heading for the target variable as a string. Defaults to target.

treat

A string indicating the grouping variable, e.g. the variable specifying the treatment population.

indent

A string to be used as indentation of summary statistics labels. Defaults to four HTML non-breaking spaces ( ).

.total_dt

Separate table from dt from which to derive total counts per group.

pct_dec

Decimal places for reported figures.

Examples

Run this code
# Calculate summary statistics of a numeric variable, e.g. `AGE`.
calc_stats(dt = adsl, "AGE", treat = "ARM")[[1]]

# Calculate summary statistics of a categorical variable, e.g. `SEX`.
calc_stats(dt = adsl, "SEX", treat = "ARM")[[1]]

Run the code above in your browser using DataLab