Learn R Programming

mStats (version 3.2.2)

expandTables: Convert 2x2 tables to data.frame

Description

expandTables() generates a data frame and supports two levels.

Usage

expandTables(
  ...,
  exp_name = "exposure",
  exp_lvl = c("exposed", "unexposed"),
  case_name = "outcome",
  case_lvl = c("disease", "healthy"),
  strata_name = "strata",
  stringsAsFactors = FALSE
)

Arguments

...

vectors of 2x2 tables

exp_name

Name of Exposure Variable

exp_lvl

Names of two categories in the order of Exposed and non-exposed

case_name

Name of Case variable

case_lvl

names of two categories in the order of

strata_name

Name of stratified variable

stringsAsFactors

TRUE or FALSE

If TRUE, character vector is converted to a factor when data.frame is constructed.

Value

data.frame

Details

expandTables uses the vectors of 2x2 tables and generates a data frame of at least two columns: Exposure and Outcome.

expandTables(c(100, 200, 100, 200))

Strata

Strata can be included using multiple named vectors. The generated tables can be used for further calculation such as Mantel Haenszel methods.

expandTables(
             strata1 = c(100, 200, 100, 200),
             strata2 = c(100, 200, 100, 200),
             strata3 = c(100, 200, 100, 200),
             exp_name = "Exposure",
             exp_lvl = c("exposed", "unexposed"),
             case_name = "Outcome",
             case_lvl = c("case", "control"),
             strata_name = "Strata"
)

Labels

If variables' names or levels are not specified, the followings are applied.

  1. Exposure Name: exposure

  2. Exposure levels: exposed and unexposed

  3. Outcome Name: outcome

  4. Outcome levels: disease and healthy

  5. Strata Name: strata

  6. Note: Strata levels are not considered as vectors must be named.

Examples

Run this code
# NOT RUN {

## Asthma Example from Essential Medical Statistics
## page 160

asthma <- expandTables(c(81, 995, 57, 867),
              exp_name = "sex",
              exp_lvl = c("woman", "man"),
              case_name = "asthma",
              case_lvl = c("yes", "no"))

## label variable and dataset
asthma <- labelData(asthma, "Hypothetical Data of Asthma Prevalence")
asthma <- labelVar(asthma, c(sex, asthma), c("Man or Woman", "Asthma or No Asthma"))

## Checking codebook
codebook(asthma)


## simple tabulation
tab(asthma)

## cross-tabulation
tab(asthma, sex, by = asthma)


# }

Run the code above in your browser using DataLab