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.
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
.
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.
Examples
qtable(ex_adsl)
#> all obs
#> (N=400)
#> ———————————————
#> count 400
qtable(ex_adsl, row_vars = "ARM")
#> all obs
#> count (N=400)
#> ————————————————————————
#> A: Drug X 134
#> B: Placebo 134
#> C: Combination 132
qtable(ex_adsl, col_vars = "ARM")
#> A: Drug X B: Placebo C: Combination
#> (N=134) (N=134) (N=132)
#> ———————————————————————————————————————————————
#> count 134 134 132
qtable(ex_adsl, row_vars = "SEX", col_vars = "ARM")
#> A: Drug X B: Placebo C: Combination
#> count (N=134) (N=134) (N=132)
#> ——————————————————————————————————————————————————————————
#> F 79 77 66
#> M 51 55 60
#> U 3 2 4
#> UNDIFFERENTIATED 1 0 2
qtable(ex_adsl, row_vars = c("COUNTRY", "SEX"), col_vars = c("ARM", "STRATA1"))
#> A: Drug X B: Placebo C: Combination
#> A B C A B C A B C
#> count (N=38) (N=47) (N=49) (N=44) (N=45) (N=45) (N=40) (N=43) (N=49)
#> ———————————————————————————————————————————————————————————————————————————————————————————————————
#> CHN
#> F 11 15 18 12 15 18 8 9 12
#> M 9 7 11 8 12 15 10 10 10
#> U 1 0 1 1 0 0 1 0 2
#> UNDIFFERENTIATED 0 0 1 0 0 0 1 0 1
#> USA
#> F 1 3 3 1 4 3 2 4 4
#> M 1 2 0 3 1 1 1 3 3
#> BRA
#> F 3 1 1 2 1 0 0 3 2
#> M 1 7 0 4 0 0 2 3 0
#> PAK
#> F 3 1 4 4 0 0 2 1 1
#> M 2 1 1 2 2 1 1 3 2
#> NGA
#> F 0 1 2 2 1 2 1 0 4
#> M 2 1 1 0 1 1 4 1 1
#> U 0 1 0 0 0 0 0 0 0
#> RUS
#> F 0 2 2 2 1 2 1 1 2
#> M 1 0 0 1 1 1 0 0 1
#> U 0 0 0 0 0 0 0 1 0
#> JPN
#> F 1 1 2 0 2 1 3 2 2
#> M 0 1 0 0 0 0 1 0 1
#> U 0 0 0 0 1 0 0 0 0
#> GBR
#> F 1 0 1 0 2 0 0 1 0
#> M 0 1 1 1 0 0 0 0 1
#> CAN
#> F 1 1 0 1 1 0 1 0 0
#> M 0 1 0 0 0 0 1 1 0
qtable(ex_adsl,
row_vars = c("COUNTRY", "SEX"),
col_vars = c("ARM", "STRATA1"), avar = "AGE", afun = mean
)
#> A: Drug X B: Placebo C: Combination
#> A B C A B C A B C
#> AGE - mean (N=38) (N=47) (N=49) (N=44) (N=45) (N=45) (N=40) (N=43) (N=49)
#> ———————————————————————————————————————————————————————————————————————————————————————————————————
#> CHN
#> F 31.09 30.67 32.56 33.08 35.73 35.28 35.25 33.89 35.75
#> M 34.44 36.43 35.73 38.38 35.25 37.60 30.80 35.20 38.60
#> U 33.00 NA 34.00 27.00 NA NA 38.00 NA 33.00
#> UNDIFFERENTIATED NA NA 28.00 NA NA NA 44.00 NA 46.00
#> USA
#> F 24.00 38.00 35.00 46.00 40.75 32.33 36.50 30.50 36.00
#> M 40.00 34.50 NA 38.67 53.00 30.00 47.00 38.00 29.00
#> BRA
#> F 35.33 38.00 44.00 27.50 25.00 NA NA 46.33 33.00
#> M 43.00 35.14 NA 36.00 NA NA 28.50 40.33 NA
#> PAK
#> F 28.00 38.00 29.50 30.75 NA NA 33.00 23.00 49.00
#> M 39.00 37.00 31.00 41.50 28.50 33.00 40.00 35.33 35.50
#> NGA
#> F NA 25.00 35.00 26.50 37.00 32.00 31.00 NA 32.75
#> M 29.50 29.00 50.00 NA 40.00 24.00 34.50 32.00 28.00
#> U NA 28.00 NA NA NA NA NA NA NA
#> RUS
#> F NA 36.50 39.50 31.00 36.00 39.50 30.00 44.00 26.00
#> M 40.00 NA NA 36.00 58.00 29.00 NA NA 28.00
#> U NA NA NA NA NA NA NA 37.00 NA
#> JPN
#> F 29.00 34.00 37.50 NA 29.00 28.00 32.33 47.50 34.00
#> M NA 48.00 NA NA NA NA 45.00 NA 26.00
#> U NA NA NA NA 35.00 NA NA NA NA
#> GBR
#> F 28.00 NA 36.00 NA 28.00 NA NA 40.00 NA
#> M NA 27.00 28.00 62.00 NA NA NA NA 69.00
#> CAN
#> F 41.00 39.00 NA 34.00 43.00 NA 37.00 NA NA
#> M NA 31.00 NA NA NA NA 36.00 32.00 NA
summary_list <- function(x, ...) as.list(summary(x))
qtable(ex_adsl, row_vars = "SEX", col_vars = "ARM", avar = "AGE", afun = summary_list)
#> A: Drug X B: Placebo C: Combination
#> AGE - summary_list (N=134) (N=134) (N=132)
#> ————————————————————————————————————————————————————————————
#> F
#> Min. 21.00 23.00 21.00
#> 1st Qu. 28.00 29.00 31.25
#> Median 32.00 32.00 35.00
#> Mean 32.76 34.12 35.20
#> 3rd Qu. 37.00 37.00 38.00
#> Max. 47.00 58.00 64.00
#> M
#> Min. 23.00 21.00 20.00
#> 1st Qu. 29.50 32.50 29.00
#> Median 37.00 37.00 33.50
#> Mean 35.57 37.44 35.38
#> 3rd Qu. 40.50 41.50 40.00
#> Max. 50.00 62.00 69.00
#> U
#> Min. 28.00 27.00 31.00
#> 1st Qu. 30.50 29.00 34.00
#> Median 33.00 31.00 36.00
#> Mean 31.67 31.00 35.25
#> 3rd Qu. 33.50 33.00 37.25
#> Max. 34.00 35.00 38.00
#> UNDIFFERENTIATED
#> Min. 28.00 NA 44.00
#> 1st Qu. 28.00 NA 44.50
#> Median 28.00 NA 45.00
#> Mean 28.00 NA 45.00
#> 3rd Qu. 28.00 NA 45.50
#> Max. 28.00 NA 46.00
suppressWarnings(qtable(ex_adsl,
row_vars = "SEX",
col_vars = "ARM", avar = "AGE", afun = range
))
#> A: Drug X B: Placebo C: Combination
#> AGE - range (N=134) (N=134) (N=132)
#> —————————————————————————————————————————————————————————————
#> F 21.0 / 47.0 23.0 / 58.0 21.0 / 64.0
#> M 23.0 / 50.0 21.0 / 62.0 20.0 / 69.0
#> U 28.0 / 34.0 27.0 / 35.0 31.0 / 38.0
#> UNDIFFERENTIATED 28.0 / 28.0 Inf / -Inf 44.0 / 46.0