Learn R Programming

abd (version 0.1-22)

expand.dft: Expand a data.frame

Description

This function expands a data.frame based on a column (col.exp). Useful for generating long tables from contingency tables.

Usage

expand.dft(x, col.exp, na.strings = "NA", as.is = FALSE, dec = ".")

Arguments

x
a data.frame
col.exp
a character string of the column name or the column number to use for "expansion"
na.strings
passed to type.convert
as.is
passed to type.convert
dec
passed to type.convert

Value

  • DFa data.frame of x in "long" format.

References

See https://stat.ethz.ch/pipermail/r-help/2009-January/185561.html for discussion of expand.dft().

See Also

table

Examples

Run this code
data(Aspirin)
Aspirin

# Specifying col.exp as character
Aspirin.expanded <- expand.dft(Aspirin, "count")
str(Aspirin.expanded)
xtabs(~treatment + cancer, Aspirin.expanded)

# Specifying col.exp as numeric
Aspirin.expanded <- expand.dft(Aspirin, 3)
str(Aspirin.expanded)
xtabs(~treatment + cancer, Aspirin.expanded)

# Plot 2X2 Contingency tables
plot( ~ treatment + cancer, data = Aspirin.expanded)
plot(table(Aspirin.expanded), main = "")
mosaicplot(~treatment + cancer, Aspirin.expanded)

# much nicer looking plots using vcd
if(require(vcd)) {
  mosaic(~treatment + cancer, Aspirin.expanded)
}

Run the code above in your browser using DataLab