tabler_stat2: 'tabler_stat' wrappers

View source: R/utils2.R

tabler_stat2R Documentation

tabler_stat wrappers

Description

Helper functions for using tabler_stat.

Usage

tabler_stat2(
  data,
  varname,
  byvar = NULL,
  varname_label = names(varname),
  byvar_label = names(byvar),
  digits = NULL,
  FUN = NULL,
  confint = FALSE,
  total = TRUE,
  n = NULL,
  include_na_in_prop = TRUE,
  iqr = FALSE,
  format_pval = TRUE,
  color_pval = TRUE,
  correct = FALSE,
  color_missing = TRUE,
  dagger = TRUE,
  group = NULL,
  color_cell_by = "none",
  cell_color = palette()[1:2],
  statArgs = NULL,
  align = NULL,
  rgroup = NULL,
  cgroup = NULL,
  tfoot = NULL,
  tfoot2 = NULL,
  htmlArgs = NULL,
  zeros = "-",
  clean_daggers = FALSE
)

Arguments

data

a matrix or data frame with variables varname and byvar

varname

one or more variables in data to calculate statistics by byvar; the rows variable(s) of the table

byvar

a stratification variable; the column variable of the table

varname_label, byvar_label

optional labels for each varname and byvar

note that duplicated varname_labels will be processed individually then grouped; this is useful if one variable is shown as continuous and categorical, for example; grouped variables should be consecutive, see examples

digits

NULL or a vector of digits past the decimal point to keep for each varname; if NULL, these will be guessed using rawr:::guess_digits; if length 1, all will be rounded to digits

alternatively, to set digits for a single varname manually, a named vector of digits corresponding to one or more varname variables can be used; see examples

FUN

NULL or a list of functions performing the test of association between each varname and byvar; see tabler_stat

confint

optional vector of varname(s) to summarize as confidence intervals

total

logical; if TRUE, total column will be shown

n

(optional) the sample size for each column used to calculate percents; if length 1, recycled as necessary; if length > 1, length must be the same as the number of columns in the table including total

include_na_in_prop

logical; if TRUE (default), the number of missing values is included when calculating proportions for factor levels; if FALSE, only non-missing levels count towards proportions

iqr

logical; if TRUE, the interquartile range is used instead of the full range (default) for continuous variables

format_pval

logical; if TRUE, p-values will be formatted using pvalr; alternatively, a function may by used which will be applied to each p-value

color_pval, color_missing, dagger

NULL or vectors, recycled as needed for each varname; see tabler_stat

correct

logical or one of p.adjust.methods; if p-value correction is desired, a column is added to the table with the corrected p-values

group

a named vector of variables (either index of or a value in varname) to begin a new group (see tspanner in htmlTable)

color_cell_by, cell_color

apply a color gradient (cell_color) to each cell (for html output); one of "none", "value", or "pct"; see tabler_stat

statArgs

a named list of additional arguments passed to getDescriptionStatsBy

align, rgroup, cgroup, tfoot

optional arguments passed to htmlTable; if cgroup = FALSE, no column spanners will be shown

tfoot2

optional footnote(s) appended to tfoot

htmlArgs

a named list of additional arguments passed to htmlTable

zeros

a character string used in place of zero cells (non-character value keeps cells as-is)

clean_daggers

logical or one of "letters", "numbers"; values other than FALSE will replace default dagger characters

See Also

tabler_stat; rawr:::get_tabler_stat_n

rawr:::guess_digits; rawr:::name_or_index

rawr:::tabler_stat_list; rawr:::tabler_stat_html

Other tabler: tabler_by(), tabler_resp(), tabler_stat(), tabler()

Examples

sapply(mtcars[1:6], rawr:::guess_digits)
Map(rawr::roundr, mtcars[1:6], sapply(mtcars[1:6], rawr:::guess_digits))

rawr:::get_tabler_stat_n(mtcars$gear)


## typical usage
mt <- within(mtcars, {
  cyl  <- factor(cyl)
  mpg2 <- factor(+(mpg > 20), 0:1, c('&le; 20', '&gt; 20'))
})

tabler_stat2(mt, c('mpg', 'cyl', 'wt'))
tabler_stat2(mt, c('mpg', 'cyl', 'wt'), 'vs')
tabler_stat2(mt, c('mpg', 'cyl', 'wt'), 'vs', group = 'wt')
tabler_stat2(mt, c('mpg', 'cyl', 'wt'), 'vs',
  group = c(group1 = 1, 'Weight' = 3))
tabler_stat2(mt, c('mpg', 'cyl', 'wt'), 'vs',
  FUN = list('kruskal', 'fisher', FALSE))

tabler_stat2(
 mt, c('mpg', 'cyl', 'wt'), 'vs',
 cgroup = c('', 'V/S engine', ''),
 rgroup = c('Miles/gallon', 'No. cylinders', 'Weight (1000 lbs)')
)

tabler_stat2(
  mt,
  varname = c(MPG = 'mpg', MPG = 'mpg2', Cylinders = 'cyl', Weight = 'wt'),
  byvar = c('V/S engine' = 'vs'),
  confint = c('mpg2', 'cyl'),
  zeros = NULL,
  digits = c(wt = 2)
  # digits = c('3' = 2) ## equivalently
)


## multiple column variables
tabler_stat2(mt, c('mpg', 'cyl', 'wt'), c('vs', 'gear'), FUN = NA)


## Not run: 
## experimental -- row- or column-merge tables
h1 <- tabler_stat2(mt, c('mpg', 'cyl', 'wt'), 'vs')
h2 <- tabler_stat2(mt, c('mpg', 'cyl', 'wt'), 'am')
h3 <- tabler_stat2(mt, c('mpg', 'cyl', 'wt'), 'gear')

rawr:::combine_tabler_stat2(list(h1, h2), how = 'rbind')
rawr:::combine_tabler_stat2(list(table1 = h1, table2 = h2), how = 'rbind')

rawr:::combine_tabler_stat2(list(h1, h2, h3), how = 'cbind')
rawr:::combine_tabler_stat2(list(t1 = h1, t2 = h2, t3 = h3), how = 'cbind')

## End(Not run)


raredd/rawr documentation built on March 4, 2024, 1:36 a.m.