expandtbl {mStats} | R Documentation |
Expand 2x2 table
into data.frame
Description
expandtbl()
generates a data.frame based on vectors.
Usage
expandtbl(
...,
exp_name = "exp",
exp_lvl = c("exposed", "unexposed"),
case_name = "case",
case_lvl = c("case", "control"),
strata_name = "strata"
)
expandfreq(data, freq)
Arguments
... |
vectors |
exp_name |
Name of |
exp_lvl |
Names of two categories in the order of Exposed and non-exposed |
case_name |
Name of |
case_lvl |
names of two categories in the order of |
strata_name |
Name of stratified variable |
data |
frequency table in data.frame |
freq |
name of variable for the weighted frequency |
Details
expandtbl
uses the vectors of 2x2
tables and
generates a data frame of at least two columns:
exp and case.
expandtbl(c(100, 200, 100, 200))
Strata
Multiple tables can be used to construct a dataset by specifying
strata_name
as follow. Strata can be included
using multiple named vectors.
expandtbl( strata1 = c(100, 200, 100, 200), strata2 = c(100, 200, 100, 200), strata3 = c(100, 200, 100, 200), exp_name = "exp", exp_lvl = c("exposed", "unexposed"), case_name = "case", case_lvl = c("case", "control"), strata_name = "Strata" )
Labels for variables
If names or levels of variables are not specified, the followings are applied.
exp Name:
exp
exp levels:
exposed
andunexposed
case Name:
case
case levels:
case
andcontrol
Strata Name:
strata
Note: Strata levels are not considered as vectors must be named.
expandfreq() uses the weighted frequencies in
data.frame format and construct another data.frame
based on the frequency weight.
The name of the frequency weighted variable can be
specified by freq
argument.
Value
data.frame
Functions
-
expandfreq
:expandfreq()
expands a frequency-weighted table into a data.frame.
Author(s)
Email: dr.myominnoo@gmail.com
Website: https://myominnoo.github.io/
Examples
## Asthma Example from Essential Medical Statistics
## page 160
asthma <- expandtbl(c(81, 995, 57, 867),
exp_name = "sex",
exp_lvl = c("woman", "man"),
case_name = "asthma",
case_lvl = c("yes", "no"))
## Not run:
## label variable and dataset
asthma <- label(asthma, "Hypothetical Data of Asthma Prevalence")
asthma <- label(asthma, sex = "Man or Woman",
asthma = "Asthma or No Asthma")
## Checking codebook
codebook(asthma)
## simple tabulation
tab(asthma)
## cross-tabulation
tab(asthma, sex, by = asthma)
## End(Not run)
## Example for expanding frequency weighted data
## Example from UCLA website
## you can download the dataset here:
## https://stats.idre.ucla.edu/stat/stata/examples/icda/afterlife.dta
x <- data.frame(gender = c(1, 1, 0, 0),
aftlife = c(1, 0, 1, 0),
freq = c(435, 147, 375, 134))
y <- expandfreq(x, freq)
## check the numbers by tabulation
## tab(y, gender, by = aftlife)