Learn R Programming

twoxtwo (version 0.1.0)

twoxtwo: Create a twoxtwo table

Description

The twoxtwo constructor function takes an input data frame and summarizes counts of the specified exposure and outcome variables as a two-by-two contingency table. This function is used internally in other functions, but can be used on its own as well. The returned object is given a twoxtwo class which allows dispatch of the twoxtwo S3 methods (see print.twoxtwo and summary.twoxtwo).

For more information on how the two-by-two table is created see 'Details'.

Usage

twoxtwo(.data, exposure, outcome, levels = NULL, na.rm = TRUE, retain = TRUE)

Arguments

.data

Data frame with observation-level exposure and outcome data

exposure

Name of exposure variable

outcome

Name of outcome variable

levels

Levels for the exposure and outcome as a named list; if supplied, then the contingency table will be oriented with respect to the sequence of levels specified; default is NULL

na.rm

Logical as to whether or not to remove NA values when constructing contingency table; default is TRUE

retain

Logical as to whether or not the original data passed to the ".data" argument should be retained; if FALSE the summary.twoxtwo() function will not compute effect measures; default is TRUE

Value

A named list with the twoxtwo class. Elements include:

  • tbl: The summarized two-by-two contingency table as a tibble.

  • cells: Named list with the counts in each of the cells in the two-by-two contingency table (i.e. A,B,C,D)

  • exposure: Named list of exposure information (name of variable and levels)

  • outcome: Named list of outcome information (name of variable and levels)

  • n_missing: The number of missing values (in either exposure or outcome variable) removed prior to computing counts for the two-by-two table

  • data: The original data frame passed to the ".data" argument. If retain=FALSE, then this element will be NULL.

Details

The two-by-two table covers four conditions that can be specified with A,B,C,D notation:

  • A: Exposure "+" and Outcome "+"

  • B: Exposure "+" and Outcome "-"

  • C: Exposure "-" and Outcome "+"

  • D: Exposure "-" and Outcome "-"

twoxtwo() requires that the exposure and outcome variables are binary. The columns can be character, numeric, or factor but must have only two levels. Each column will internally be coerced to a factor with levels reversed. The reversal results in exposures with TRUE and FALSE (or 1 and 0) oriented in the two-by-two table with the TRUE as "+" (first row) and FALSE as "-" (second row). Likewise, TRUE/FALSE outcomes will be oriented with TRUE as "+" (first column) and FALSE as "-" (second column). Note that the user can also define the orientation of the table using the "levels" argument.