Generate Rows Analyzing Different Variables Across Columns
Source:R/colby_constructors.R
analyze_colvars.Rd
Generate Rows Analyzing Different Variables Across Columns
Usage
analyze_colvars(
lyt,
afun,
format = NULL,
na_str = NA_character_,
nested = TRUE,
extra_args = list(),
indent_mod = 0L,
inclNAs = FALSE
)
Arguments
- lyt
layout object pre-data used for tabulation
- afun
function or list. Function(s) to be used to calculate the values in each column. The list will be repped out as needed and matched by position with the columns during tabulation. This functions accepts the same parameters as analyze like
afun
andformat
. For further information seeadditional_fun_params
.- format
FormatSpec
. Format associated with this split. Formats can be declared via strings ("xx.x"
) or function. In cases such asanalyze
calls, they can character vectors or lists of functions.- na_str
character(1). String that should be displayed when the value of
x
is missing. Defaults to"NA"
.- nested
boolean. Should this layout instruction 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.
- indent_mod
numeric. Modifier for the default indent position for the structure created by this function(subtable, content table, or row) and all of that structure's children. Defaults to 0, which corresponds to the unmodified default behavior.
- inclNAs
boolean. Should observations with NA in the
var
variable(s) be included when performing this analysis. Defaults toFALSE
Value
A PreDataTableLayouts
object suitable for passing to further
layouting functions, and to build_table
.
Examples
library(dplyr)
#>
#> Attaching package: ‘dplyr’
#> The following object is masked from ‘package:testthat’:
#>
#> matches
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
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) rcell(mean(x), format = "xx.x"),
function(x) rcell(sum(x > .5), format = "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%)
#> 0.1 42 0.1 31 0.0 50
#> BLACK OR AFRICAN AMERICAN 28 (23.1%) 28 (23.1%) 24 (22.6%) 24 (22.6%) 27 (20.9%) 27 (20.9%)
#> -0.2 18 -0.1 9 0.1 12
#> WHITE 14 (11.6%) 14 (11.6%) 14 (13.2%) 14 (13.2%) 18 (14.0%) 18 (14.0%)
#> -0.0 7 0.2 11 0.1 6
lyt2 <- basic_table() %>%
split_cols_by("ARM") %>%
split_cols_by_multivar(c("value", "pctdiff"),
varlabels = c("Measurement", "Pct Diff")
) %>%
split_rows_by("RACE",
split_label = "ethnicity",
split_fun = drop_split_levels
) %>%
summarize_row_groups() %>%
analyze_colvars(afun = mean, format = "xx.xx")
tbl2 <- build_table(lyt2, ANL)
tbl2
#> A: Drug X B: Placebo C: Combination
#> Measurement Pct Diff Measurement Pct Diff Measurement Pct Diff
#> ——————————————————————————————————————————————————————————————————————————————————————————————————————————
#> ASIAN 79 (65.3%) 79 (65.3%) 68 (64.2%) 68 (64.2%) 84 (65.1%) 84 (65.1%)
#> mean 0.11 0.51 0.08 0.48 0.03 0.54
#> BLACK OR AFRICAN AMERICAN 28 (23.1%) 28 (23.1%) 24 (22.6%) 24 (22.6%) 27 (20.9%) 27 (20.9%)
#> mean -0.17 0.62 -0.15 0.42 0.14 0.44
#> WHITE 14 (11.6%) 14 (11.6%) 14 (13.2%) 14 (13.2%) 18 (14.0%) 18 (14.0%)
#> mean -0.01 0.51 0.23 0.69 0.11 0.40