tab1: Generate an flextable of descriptive statistics.

Description Usage Arguments Details Value Examples

View source: R/tab1.R

Description

There are two interfaces, the default, which typically takes a list of data.frames for x, and the formula interface. The formula interface is less flexible, but simpler to use and designed to handle the most common use cases. It is important to use factors appropriately for categorical variables (i.e. have the levels labeled properly and in the desired order). The contents of the table can be customized by providing user-defined ‘renderer’ functions. To facilitate this, some tags (such as row labels) are given specific classes for easy CSS selection.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
tab1(
  vars,
  data,
  group = NULL,
  row_split = NULL,
  overall = TRUE,
  select = NULL,
  render = render.default,
  drop_lev = TRUE,
  indent = "  ",
  ...
)

Arguments

vars

Variables to be used for summary table.

data

A data.frame from which the variables in vars should be taken.

group

Name of the grouping variable.

row_split

Variable that used for splitting table rows, rows will be splited using this variable. Useful for repeated measures.

overall

A label for the "Total" column. Specify NULL or FALSE to omit the column altogether.

select

a named vector with as many components as row-variables. Every element of 'select' will be used to select the individuals to be analyzed for every row-variable. Name of the vector corresponds to the row variable, element is the selection.

render

A function to render the table cells (see Details).

drop_lev

Should empty factor levels be dropped?

indent

Indent symbol of the table for statistic values.

...

Further arguments, passed to render.

Details

For the default version, is is expected that x is a named list of data.frames, one for each stratum, with names corresponding to strata labels.

Value

An object of class "tab1".

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
dat <- expand.grid(id=1:10, sex=c("Male", "Female"), treat=c("Treated", "Placebo"))
dat$age <- runif(nrow(dat), 10, 50)
dat$age[3] <- NA  # Add a missing value
dat$wt <- exp(rnorm(nrow(dat), log(70), 0.2))

var_lab(dat$sex) <- "Sex"
var_lab(dat$age) <- "Age"
var_lab(dat$treat) <- "Treatment Group"
var_lab(dat$wt) <- "Weight"


# Something more complicated

dat$dose <- ifelse(dat$treat=="Placebo", "Placebo",
                   sample(c("5 mg", "10 mg"), nrow(dat), replace=TRUE))
dat$dose <- factor(dat$dose, levels=c("Placebo", "5 mg", "10 mg"))



my.render.cont <- function(x) {
    with(stats.default(x),
        sprintf("%0.2f (%0.1f)", MEAN, SD))
}

tab1(c("age", "sex", "wt"),
data = dat,
group = "treat",
 render.continuous=my.render.cont)

adayim/cttab documentation built on Dec. 18, 2021, 10:27 p.m.