Learn R Programming

DescTools (version 0.99.19)

TOne: Create Table One Describing Baseline Characteristics

Description

Create a table summarizing continuous, categorical and dichotomous variables, optionally stratified by one or more variables, while performing adequate statistical tests.

Usage

TOne(x, grp = NA, add.length = TRUE, colnames = NULL, vnames = NULL, total = TRUE, align = "\\l", FUN = NULL, NUMTEST = NULL, numtestlab = NULL)

Arguments

x
a data.frame containing all the variables to be included in the table.

grp
the grouping variable.

add.length
logical. If set to TRUE (default), a row with the lengths of the group will be inserted as first row of the table.

colnames
a vector of columnnames for the result table.

vnames
a vector of variablenames to be placed in the first column instead of the real names.

total
logical, TRUE if totals should be added.
align
the character on whose position the strings will be aligned. Left alignment can be requested by setting sep = "\\l", right alignment by "\\r" and center alignment by "\\c". Mind the backslashes, as if they are omitted, strings would be aligned to the character l, r or c respectively. Default value is "\\l", thus left alignment.

FUN
the function to be used as location and dispersion measure (typically mean/sd or median/IQR)
NUMTEST
the function to be used to compare location (default kruskal.test). Must be defined as function with arguments (x, g), generating something similar to a p.value. (See examples.)
numtestlab
string, describing the test for numeric variables. Default is "Kruskal-Wallis test". Alternative could be "ANOVA".

Value

Details

There are three types of variables currently implemented: numeric, factor and variables with only two levels.

See Also

WrdTable

Examples

Run this code
opt <- DescToolsOptions()

# define some special formats for count data, percentages and numeric results
# (those will be supported by TOne)
Fmt(abs=as.fmt(digits=0, big.mark="'"))
Fmt(per=as.fmt(digits=1, fmt="%"))
Fmt(num=as.fmt(digits=1, big.mark="'"))

TOne(x = d.pizza[, c("temperature","delivery_min","driver","wine_ordered")],
     grp=d.pizza$quality)

# define median/IQR as describing functions for the numeric variables
TOne(iris[, -5], iris[, 5],
  FUN = function(x) gettextf("%s / %s",
         Format(median(x, na.rm=TRUE), digits=1),
         Format(IQR(x, na.rm=TRUE), digits=3))
)

# replace kruskal.test by ANOVA and report the p.value
TOne(x = iris[, -5], grp = iris[, 5],
     FUN = function(x) gettextf("%s / %s",
            Format(mean(x, na.rm=TRUE), digits=1),
            Format(sd(x, na.rm=TRUE), digits=3)),
     NUMTEST = function(x, g)
            Format(summary(aov(x ~ g))[[1]][1, "Pr(>F)"], fmt="p"),
     numtestlab = "ANOVA"
)

options(opt)

## Not run:   --  Send the whole stuff to Word
# 
# wrd <- GetNewWrd()
# ToWrd(TOne(x = d.pizza[, c("temperature","delivery_min","driver","wine_ordered")],
#            grp=d.pizza$quality))
#            ## End(Not run)

Run the code above in your browser using DataLab