View source: R/tt_dotabulation.R
build_table | R Documentation |
Layouts are used to describe a table pre-data. build_table
is used to
create a table using a layout and a dataset.
build_table(
lyt,
df,
alt_counts_df = NULL,
col_counts = NULL,
col_total = if (is.null(alt_counts_df)) nrow(df) else nrow(alt_counts_df),
topleft = NULL,
hsep = default_hsep(),
...
)
lyt |
layout object pre-data used for tabulation |
df |
dataset ( |
alt_counts_df |
dataset ( |
col_counts |
numeric (or |
col_total |
integer(1). The total observations across all columns.
Defaults to |
topleft |
character. Override values for the "top left" material to be displayed during printing. |
hsep |
character(1). Set of character(s) to be repeated as the separator
between the header and body of the table when rendered as text. Defaults to
a connected horizontal line (unicode 2014) in locals that use a UTF
charset, and to |
... |
currently ignored. |
When alt_counts_df
is specified, column counts are calculated by
applying the exact column subsetting expressions determined when applying
column splitting to the main data (df
) to alt_counts_df
and
counting the observations in each resulting subset.
In particular, this means that in the case of splitting based on cuts of the
data, any dynamic cuts will have been calculated based on df
and
simply re-used for the count calculation.
A TableTree
or ElementaryTable
object representing the
table created by performing the tabulations declared in lyt
to the
data df
.
When overriding the column counts or totals care must be taken that,
e.g., length()
or nrow()
are not called within tabulation functions,
because those will NOT give the overridden counts. Writing/using tabulation
functions which accept .N_col
and .N_total
or do not rely on
column counts at all (even implicitly) is the only way to ensure overridden
counts are fully respected.
Gabriel Becker
lyt <- basic_table() %>%
split_cols_by("Species") %>%
analyze("Sepal.Length", afun = function(x) {
list(
"mean (sd)" = rcell(c(mean(x), sd(x)), format = "xx.xx (xx.xx)"),
"range" = diff(range(x))
)
})
lyt
tbl <- build_table(lyt, iris)
tbl
# analyze multiple variables
lyt2 <- basic_table() %>%
split_cols_by("Species") %>%
analyze(c("Sepal.Length", "Petal.Width"), afun = function(x) {
list(
"mean (sd)" = rcell(c(mean(x), sd(x)), format = "xx.xx (xx.xx)"),
"range" = diff(range(x))
)
})
tbl2 <- build_table(lyt2, iris)
tbl2
# an example more relevant for clinical trials with column counts
lyt3 <- basic_table(show_colcounts = TRUE) %>%
split_cols_by("ARM") %>%
analyze("AGE", afun = function(x) {
setNames(as.list(fivenum(x)), c("minimum", "lower-hinge", "median",
"upper-hinge", "maximum"))
})
tbl3 <- build_table(lyt3, DM)
tbl3
tbl4 <- build_table(lyt3, subset(DM, AGE > 40))
tbl4
# with column counts calculated based on different data
miniDM <- DM[sample(1:NROW(DM), 100),]
tbl5 <- build_table(lyt3, DM, alt_counts_df = miniDM)
tbl5
tbl6 <- build_table(lyt3, DM, col_counts = 1:3)
tbl6
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.