In some cases, the variable to be ultimately analyzed is most naturally defined on a column, not a row, basis.
When we need columns to reflect different variables entirely, rather than different levels of a single
variable, we use split_cols_by_multivar
.
Usage
split_cols_by_multivar(
lyt,
vars,
split_fun = NULL,
varlabels = vars,
varnames = NULL,
nested = TRUE,
extra_args = list()
)
Arguments
- lyt
(
PreDataTableLayouts
)
layout object pre-data used for tabulation.- vars
(
character
)
vector of variable names.- split_fun
(
function
orNULL
)
custom splitting function. See custom_split_funs.- varlabels
(
character
)
vector of labels forvars
.- varnames
(
character
)
vector of names forvars
which will appear in pathing. Whenvars
are all unique this will be the variable names. If not, these will be variable names with suffixes as necessary to enforce uniqueness.- nested
(
logical
)
whether this layout instruction should be applied within the existing layout structure if possible (TRUE
, the default) or as a new top-level element (FALSE
). Ignored if it would nest a split underneath analyses, which is not allowed.- extra_args
(
list
)
extra arguments to be passed to the tabulation function. Element position in the list corresponds to the children of this split. Named elements in the child-specific lists are ignored if they do not match a formal argument of the tabulation function.
Value
A PreDataTableLayouts
object suitable for passing to further layouting functions, and to build_table()
.
Examples
library(dplyr)
ANL <- DM %>% mutate(value = rnorm(n()), pctdiff = runif(n()))
## toy example where we take the mean of the first variable and the
## count of >.5 for the second.
colfuns <- list(
function(x) in_rows(mean = mean(x), .formats = "xx.x"),
function(x) in_rows("# x > 5" = sum(x > .5), .formats = "xx")
)
lyt <- basic_table() %>%
split_cols_by("ARM") %>%
split_cols_by_multivar(c("value", "pctdiff")) %>%
split_rows_by("RACE",
split_label = "ethnicity",
split_fun = drop_split_levels
) %>%
summarize_row_groups() %>%
analyze_colvars(afun = colfuns)
lyt
#> A Pre-data Table Layout
#>
#> Column-Split Structure:
#> ARM (lvls) -> value:pctdiff (vars)
#>
#> Row-Split Structure:
#> RACE (lvls) -> NA (** col-var analysis **)
#>
tbl <- build_table(lyt, ANL)
tbl
#> A: Drug X B: Placebo C: Combination
#> value pctdiff value pctdiff value pctdiff
#> ———————————————————————————————————————————————————————————————————————————————————————————————————————
#> ASIAN 79 (65.3%) 79 (65.3%) 68 (64.2%) 68 (64.2%) 84 (65.1%) 84 (65.1%)
#> mean 0.0 39 -0.1 31 -0.0 33
#> BLACK OR AFRICAN AMERICAN 28 (23.1%) 28 (23.1%) 24 (22.6%) 24 (22.6%) 27 (20.9%) 27 (20.9%)
#> mean 0.2 11 0.1 10 -0.3 10
#> WHITE 14 (11.6%) 14 (11.6%) 14 (13.2%) 14 (13.2%) 18 (14.0%) 18 (14.0%)
#> mean 0.3 8 0.4 7 -0.2 9