qtable_layout: Generalized Frequency Table

View source: R/tt_dotabulation.R

qtable_layoutR Documentation

Generalized Frequency Table

Description

This function provides a convenience interface for generating generalizations of a 2-way frequency table. Row and column space can be facetted by variables, and an analysis function can be specified.

The function then builds a layout with the specified layout and applies it to the data provided.

Usage

qtable_layout(
  data,
  row_vars = character(),
  col_vars = character(),
  avar = NULL,
  row_labels = NULL,
  afun = NULL,
  summarize_groups = FALSE,
  title = "",
  subtitles = character(),
  main_footer = character(),
  prov_footer = character(),
  show_colcounts = TRUE,
  drop_levels = TRUE,
  ...,
  .default_rlabel = NULL
)

qtable(
  data,
  row_vars = character(),
  col_vars = character(),
  avar = NULL,
  row_labels = NULL,
  afun = NULL,
  summarize_groups = FALSE,
  title = "",
  subtitles = character(),
  main_footer = character(),
  prov_footer = character(),
  show_colcounts = TRUE,
  drop_levels = TRUE,
  ...
)

Arguments

data

data.frame. The data to tabulate.

row_vars

character. The names of variables to be used in row facetting.

col_vars

character. The names of variables to be used in column facetting.

avar

character(1). The variable to be analyzed. Defaults to the first variable in data.

row_labels

character or NULL. Row label(s) which should be applied to the analysis rows. length must match the number of rows generated by afun. See details.

afun

function. The function to generate the analysis row cell values. This can be a proper analysis function, or a function which returns a vector or list. Vectors are taken as multi-valued single cells, whereas lists are interpreted as multiple cells.

summarize_groups

logical(1). Should each level of nesting include marginal summary rows. Defaults to FALSE

title

character(1). Main title (main_title()) is a single string. Ignored for subtables.

subtitles

character. Subtitles (subtitles()) can be vector of strings, where every element is printed in a separate line. Ignored for subtables.

main_footer

character. Main global (non-referential) footer materials (main_footer()). If it is a vector of strings, they will be printed on separate lines.

prov_footer

character. Provenance-related global footer materials (prov_footer()). It can be also a vector of strings, printed on different lines. Generally should not be modified by hand.

show_colcounts

logical(1). Should column counts be displayed in the resulting table when this layout is applied to data

drop_levels

logical(1). Should unobserved factor levels be dropped during facetting. Defaults to TRUE.

...

passed to afun, if specified. Otherwise ignored.

.default_rlabel

character(1). This is an implementation detail that should not be set by end users.

Details

This function creates a table with a single top-level structure in both row and column dimensions involving faceting by 0 or more variables in each.

The display of the table depends on certain details of the tabulation. In the case of an afun which returns a single cell's contents (either a scalar or a vector of 2 or 3 elements), the label rows for the deepest-nested row facets will be hidden and the labels used there will be used as the analysis row labels. In the case of an afun which returns a list (corresponding to multiple cells), the names of the list will be used as the analysis row labels and the deepest-nested facet row labels will be visible.

The table will be annotated in the top-left area with an informative label displaying the analysis variable (avar), if set, and the function used (captured via substitute) where possible, or 'count' if not. One exception where the user may directly modify the top-left area (via row_labels) is the case of a table with row facets and an afun which returns a single row.

Value

for qtable a built TableTree object representing the desired table, for qtable_layout, a PreDataTableLayouts object declaring the structure of the desired table, suitable for passing to build_table.

Examples


qtable(ex_adsl)
qtable(ex_adsl, row_vars = "ARM")
qtable(ex_adsl, col_vars = "ARM")
qtable(ex_adsl, row_vars = "SEX", col_vars = "ARM")
qtable(ex_adsl, row_vars = c("COUNTRY", "SEX"), col_vars = c("ARM", "STRATA1"))
qtable(ex_adsl, row_vars = c("COUNTRY", "SEX"),
       col_vars = c("ARM", "STRATA1"), avar = "AGE", afun = mean)
summary_list <- function(x, ...) as.list(summary(x))
qtable(ex_adsl, row_vars = "SEX", col_vars = "ARM", avar = "AGE", afun = summary_list)
suppressWarnings(qtable(ex_adsl, row_vars = "SEX",
                 col_vars = "ARM", avar = "AGE", afun = range))

rtables documentation built on Aug. 30, 2023, 5:07 p.m.