Learn R Programming

tab (version 4.1.1)

tabmulti: Create Table Comparing Characteristics Across Levels of a Categorical Variable

Description

Creates a table comparing multiple characteristics (e.g. median age, mean BMI, and race/ethnicity distribution) across levels of x.

Usage

tabmulti(formula = NULL, data, xvarname = NULL, yvarnames = NULL,
  ymeasures = NULL, columns = c("xgroups", "p"),
  listwise.deletion = FALSE, sep.char = ", ", xlevels = NULL,
  yvarlabels = NULL, ylevels = NULL, indent.spaces = 3,
  quantiles = NULL, quantile.vals = FALSE, latex = TRUE,
  decimals = NULL, formatp.list = NULL, n.headings = FALSE,
  print.html = FALSE, html.filename = "table1.html",
  tabmeans.list = NULL, tabmedians.list = NULL, tabfreq.list = NULL)

Arguments

formula

Formula, e.g. Age + Sex + Race + BMI ~ Group.

data

Data frame containing variables named in formula.

xvarname

Character string with name of column variable. Should be one of names(data).

yvarnames

Character vector with names of row variables. Each element should be one of names(data).

ymeasures

Character vector specifying whether each y variable should be summarized by mean, median, or frequency. For example, if you want to compare frequencies for the first variable, means for the second, and medians for the third, you would set ymeasures = c("freq", "mean", "median"). If unspecified, function compares means for numeric variables and frequencies for factor and character variables.

columns

Character vector specifying what columns to include. Choices for each element are "n" for total sample size, "overall" for overall statistics, "xgroups" for x group statistics, "test" for test statistic, and "p" for p-value.

listwise.deletion

Logical value for whether observations with missing values for any y variable should be excluded entirely (as opposed to using all available data for each comparison).

sep.char

Character string with separator to place between lower and upper bound of confidence intervals. Typically "-" or ", ".

xlevels

Character vector with labels for the levels of x, used in column headings.

yvarlabels

Named list specifying labels for certain y variables. For example, if you want variables named "race" and "age_yrs" to print as "Race/ethnicity" and "Age (years)", use \codeyvarlabels = list(race = "Race/ethnicity", age_yrs = "Age (years)").

ylevels

Character vector (if only 1 frequency comparison) or list of character vectors with labels for the levels of each categorical y variable.

indent.spaces

Integer value specifying how many spaces to indent factor levels.

quantiles

Numeric value. If specified, function compares y variables across quantiles of x. For example, if x contains BMI values and yvarnames includes HDL and race, setting quantiles = 3 compares mean BMI and distribution of race across BMI tertiles.

quantile.vals

Logical value for whether labels for x quantiles should show quantile number and corresponding range, e.g. Q1 [0.00, 0.25), rather than just the quantile number.

latex

Logical value for whether to format table so it is ready for printing in LaTeX via xtable or kable.

decimals

Numeric vector specifying number of decimal places for numbers other than p-values for each y variable. Can be a single value to use for all y variables.

formatp.list

List of arguments to pass to formatp.

n.headings

Logical value for whether to display group sample sizes in parentheses in column headings.

print.html

Logical value for whether to write a .html file with the table to the current working directory.

html.filename

Character string specifying the name of the .html file that gets written if print.html = TRUE.

tabmeans.list

List of arguments to pass to tabmeans.

tabmedians.list

List of arguments to pass to tabmedians.

tabfreq.list

List of arguments to pass to tabfreq.

Value

Data frame which you can print in R (e.g. with xtable's xtable or knitr's kable) or export to Word, Excel, or some other program. To export the table, set print.html = TRUE. This will result in a .html file being written to your current working directory, which you can open and copy/paste into your document.

Examples

Run this code
# NOT RUN {
# Compare age, sex, race, and BMI in control vs. treatment group
tabmulti(Age + Sex + Race + BMI ~ Group, data = tabdata) %>%
  kable()

# Same as previous, but compare medians rather than means for BMI
tabmulti(Age + Sex + Race + BMI ~ Group, data = tabdata,
         ymeasures = c("mean", "freq", "freq", "median")) %>%
  kable()


# }

Run the code above in your browser using DataLab