Skip to contents

These functions create summary tables from a data.frame using a formula interface. Various aggregation methods are available:

  • create_table_count() counts rows per group.

  • create_table_sum() sums a variable per group.

  • create_table_mean() computes the mean of a variable per group.

  • create_table_quantile() computes a specific quantile of a variable per group.

  • create_table_median() computes the median of a variable per group.

  • create_table_max() computes the maximum of a variable per group.

  • create_table_min() computes the minimum of a variable per group.

  • create_table_custom() applies a user-supplied function per group.

Usage

create_table_count(
  df,
  formula,
  ...,
  where = NULL,
  w = NULL,
  rep_w = NULL,
  check = getOption("procr.create_table.check", FALSE),
  env = parent.frame()
)

create_table_sum(
  df,
  formula,
  var,
  ...,
  where = NULL,
  w = NULL,
  rep_w = NULL,
  check = getOption("procr.create_table.check", FALSE),
  env = parent.frame()
)

create_table_mean(
  df,
  formula,
  var,
  ...,
  where = NULL,
  w = NULL,
  rep_w = NULL,
  check = getOption("procr.create_table.check", FALSE),
  env = parent.frame()
)

create_table_quantile(
  df,
  formula,
  var,
  prob,
  ...,
  qtype = 7L,
  where = NULL,
  w = NULL,
  rep_w = NULL,
  check = getOption("procr.create_table.check", FALSE),
  env = parent.frame()
)

create_table_median(
  df,
  formula,
  var,
  ...,
  qtype = 7L,
  where = NULL,
  w = NULL,
  rep_w = NULL,
  check = getOption("procr.create_table.check", FALSE),
  env = parent.frame()
)

create_table_max(
  df,
  formula,
  var,
  ...,
  where = NULL,
  w = NULL,
  rep_w = NULL,
  check = getOption("procr.create_table.check", FALSE),
  env = parent.frame()
)

create_table_min(
  df,
  formula,
  var,
  ...,
  where = NULL,
  w = NULL,
  rep_w = NULL,
  check = getOption("procr.create_table.check", FALSE),
  env = parent.frame()
)

create_table_custom(
  df,
  formula,
  fun,
  ...,
  where = NULL,
  rep_w = NULL,
  check = getOption("procr.create_table.check", FALSE),
  env = parent.frame()
)

Arguments

df

A data.frame containing the data to summarize.

formula

A formula specifying row and column grouping (e.g., var1 * var2 ~ var3 + var4).

...

Not used for values, forces later arguments to bind by name.

where

(Optional) Logical expression to filter rows before summarization. Must be an unquoted call.

w

(Optional) Weights variabel for weighted summaries. Must be a symbol.

rep_w

(Optional) A data.frame of replicate weights with as many rows as df. If replicate weights are supplied, standard errors are calculated using Welford's online algorithm. Currently only supported for create_table_count(), create_table_sum() and create_table_mean().

check

(Optional) Should the checks in check_format() be run? Defaults to getOption("procr.create_table.check", FALSE). Checks are implemented such that running them should not add much overhead.

env

(Optional) Environment in which to evaluate the formula and variables. Defaults to parent.frame().

var

Variable to summarize (required for sum, mean, median, quantile). Must be a symbol.

prob

Numeric probability with 0 < prob < 1 for `create_table_quantile()“.

qtype

(Optional) Integer 4-9 indicating the quantile type (default 7) for quantile computations. See quantile() and collapse::.quantile().

fun

A custom function to apply for create_table_custom(). Should accept a subset of the dataset and return a single value. Variable names in the dataset should be used unquoted.

Value

A procr_table object.

Details

  • Quantiles: create_table_quantile() and create_table_median() internally use collapse::.quantile() to compute quantiles. Note that quantile values may depend on the order of the input data, especially if there are relatively few var values relative to the number of rows in df. See this blog post for a more detailed explanation.

  • NA values: All functions automatically drop NA values in var and w before aggregating. create_table_custom() does not automatically remove NAs (since the measure variable is not known), leaving NA-handling to the custom function.

  • Replicate weights: Replicate weights must be specified as follows: (1) NA values may only occur in rows where is.na(w). (2) Weights for observations that were not sampled in the respective bootstrap runs must be specified as 0. For better performance, this is only checked by asserting that no NA values remain in rep_w after subsetting those rows in df that fulfill where and !is.na(w).

Examples

# Create sample format
cyl_f <- new_format(. < 5 ~ "< 5", . > 5 ~ "> 5", var = cyl)
gear_f <- new_format(. == 3 ~ "3", . == 4 ~ "4", . == 5 ~ "5", var = gear)

# Count example
tab_count <- create_table_count(mtcars, cyl_f ~ gear_f)

# Mean example
tab_mean <- create_table_mean(mtcars, cyl_f ~ gear_f, mpg)

# Custom function example
tab_custom <- create_table_custom(
  mtcars,
  cyl_f ~ gear_f,
  fun = function(drat, carb) max(drat, carb)
)

# Quantile example
tab_quantile <- create_table_quantile(mtcars, cyl_f ~ gear_f, mpg, prob = 0.75)