table1 function - RDocumentation (2024)

Description

Produces a nicely formatted table of descriptive statistics for any numberof numeric or categorical variables, optionally stratified by a factor.

Usage

table1(x, ...)

# S3 method for defaulttable1( x, labels, groupspan = NULL, rowlabelhead = "", transpose = FALSE, topclass = "Rtable1", footnote = NULL, caption = NULL, render = render.default, render.strat = render.strat.default, extra.col = NULL, extra.col.pos = NULL, ...)

# S3 method for formulatable1( x, data, overall = "Overall", rowlabelhead = "", transpose = FALSE, droplevels = TRUE, topclass = "Rtable1", footnote = NULL, caption = NULL, render = render.default, render.strat = render.strat.default, extra.col = NULL, extra.col.pos = NULL, ...)

Value

An object of class "table1".

Arguments

x

An object, typically a formula or list of data.frames (see Details).

...

Further arguments, passed to render.

labels

A list containing labels for variables, strata and groups (see Details).

groupspan

A vector of integers specifying the number of strata to group together.

rowlabelhead

A heading for the first column of the table, which contains the row labels.

transpose

Logical. Should the table be transposed (i.e. strata asrows and variables as columns)?

topclass

A class attribute for the outermost (i.e. <table>) tag.

footnote

A character string to be added as a footnote to the table.Can also be a vector which results in multiple lines of footnotes.The default NULL causes the footnote to be omitted.

caption

A character string to be added as a caption to the table.The default NULL causes the caption to be omitted.

render

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

render.strat

A function to render the stratum labels. Accepts 3arguments: the stratum label, the stratum size (number of observations), anda flag indicating whether we are in transpose mode or not. Seerender.strat.default for an example.

extra.col

An optional names list of functions that produce extra columns in the table (see Details).

extra.col.pos

An optional integer vector given the positions of extra columns (see Details).

data

For the formula interface, a data.frame from which thevariables in x should be taken.

overall

A label for the "Overall" column. Specify NULL orFALSE to omit the column altogether. By default, the "Overall" columnappears at the right end of the table; to place it on the left instead use anamed character with the name "left", e.g. c(left="Overall").

droplevels

Should empty factor levels be dropped?

Methods (by class)

  • table1(default): The default interface, where x is a data.frame.

  • table1(formula): The formula interface.

Details

There are two interfaces, the default, which typically takes a list ofdata.frames for x, and the formula interface. The formulainterface is less flexible, but simpler to use and designed to handle themost common use cases. It is important to use factors appropriately forcategorical variables (i.e. have the levels labeled properly and in thedesired order). The contents of the table can be customized by providinguser-defined `renderer' functions. Customization of the table appearance isdeliberately not attempted, as this is best accomplished with CSS. Tofacilitate this, some tags (such as row labels) are given specific classesfor easy CSS selection.

For the formula version, the formula is expected to be a one-sided formula,optionally with a vertical bar separating the variables that are to appearas data in the table (as rows) from those used for stratification (i.e.columns). There can be at most 2 variables for stratification (and only oneif transpose = TRUE is specified), and if 2 are specified, the secondis nested within the first. Stratification variables may not contain missingvalues. The formula may contain a dot (".") to refer to "all variables indata other than those that appear elsewhere in the formula". It islegitimate to use functions inside the formula to create new variables.

For the default version, is is expected that x is a namedlist of data.frames, one for each stratum, with names corresponding tostrata labels.

Extra columns can be added to the table using the extra.col argument.This is an optional named list of functions, with the names corresponding tothe column headings. Each function will be called once for each variableincluded in the table. Each function should expect 2 arguments, the firstbeing a list, the second the name of the variable. The contents of thelist passed in as the first argument will be the data associated with eachstratum in the table; i.e., one element for each normal column (not extracolumn). It is then up the function to compute the value to appear inthe extra column and return it as a string. By default, extra columns willbe placed to the far right, after the normal columns, in the order they arespecified in. This can be overridden, however, using theextra.col.pos vector of integer positions. For example, to place thefirst extra column in position 1 (far left), and the second extra column inposition 3, use extra.col.pos = c(1, 3); any extra columns that arenot assigned positions will be placed to the far right. A typical use casefor extra columns would be a column of p-values for differences betweenstrata. Note that this feature is not available when the optiontranspose = TRUE is specified.

Examples

Run this code

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 valuedat$wt <- exp(rnorm(nrow(dat), log(70), 0.2))label(dat$sex) <- "Sex"label(dat$age) <- "Age"label(dat$treat) <- "Treatment Group"label(dat$wt) <- "Weight"units(dat$age) <- "years"units(dat$wt) <- "kg"# One level of stratificationtable1(~ sex + age + wt | treat, data=dat)# Two levels of stratification (nesting)table1(~ age + wt | treat*sex, data=dat)# Switch the order or nestingtable1(~ age + wt | sex*treat, data=dat)# No stratificationtable1(~ treat + sex + age + wt, data=dat)# Something more complicateddat$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"))strata <- c(split(dat, dat$dose), list("All treated"=subset(dat, treat=="Treated")), list(Overall=dat))labels <- list( variables=list(sex=render.varlabel(dat$sex), age=render.varlabel(dat$age), wt=render.varlabel(dat$wt)), groups=list("", "Treated", ""))my.render.cont <- function(x) { with(stats.default(x),  sprintf("%0.2f (%0.1f)", MEAN, SD))}table1(strata, labels, groupspan=c(1, 3, 1), render.continuous=my.render.cont)# Transposed tabletable1(~ age + wt | treat, data=dat, transpose=TRUE)

Run the code above in your browser using DataCamp Workspace

table1 function - RDocumentation (2024)
Top Articles
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 6177

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.