DescTools (version 0.99.11)

PercTable: Percentage Table

Description

Prints a 2-way contingency table along with percentages, marginal, and conditional distributions. All the frequencies are nested into one single table.

Usage

## S3 method for class 'default':
PercTable(x, y = NULL, \dots)

## S3 method for class 'table':
PercTable(tab, row.vars = NULL, col.vars = 2, digits = 3, big.mark = "", pfmt = FALSE,  
          freq = TRUE, rfrq = "100", expected = FALSE, residuals = FALSE, 
          stdres = FALSE, margins = NULL, \dots)

## S3 method for class 'formula':
PercTable(formula, data, subset, na.action, \dots)

MarginTable(tab)

Arguments

x, y
objects which can be interpreted as factors (including character strings). x and y will be tabulated via table(x, y). If x is a matrix, it will be coerced to a table via as.table(x).
tab
a r x c-contingency table
row.vars
a vector of row variables (see Details).
col.vars
a vector of column variables (see Details).
digits
an integer defining with how many digits the percentages will be printed.
big.mark
character. If not empty used as mark between every 3 decimals before the decimal point.
pfmt
logical. If set to TRUE the relative frequencies' format will be xx.xxx %, with digits respected.
freq
boolean. Should absolute frequencies be included? Defaults to TRUE.
rfrq
a string with 3 characters, each of them being 1 or 0. The first position means total percentages, the second means row percentages and the third column percentages. "011" produces a table output with row and column percentages.
expected
the expected counts under the null hypothesis.
residuals
the Pearson residuals, (observed - expected) / sqrt(expected).
stdres
standardized residuals, (observed - expected) / sqrt(V), where V is the residual cell variance (for the case where x is a matrix, n * p * (1 - p) otherwise).
margins
a vector, consisting out of 1 and/or 2. Defines the margin sums to be included. 1 stands for row margins, 2 for column margins, c(1,2) for both. Default is NULL (none).
formula
a formula of the form lhs ~ rhs where lhs will be tabled versus rhs (table(lhs, rhs)).
data
an optional matrix or data frame (or similar: see model.frame) containing the variables in the formula formula. By default the variables are taken from environment(formula)
subset
an optional vector specifying a subset of observations to be used.
na.action
a function which indicates what should happen when the data contain NAs. Defaults to getOption("na.action").
...
the dots are passed from PercTable.default() to the PercTable.table().

Value

  • Returns an object of class "ftable".

Details

PercTable prints a 2-dimensional table. The absolute and relative frequencies are nested into one flat table by means of ftable. By means of row.vars, resp. col.vars, the structure of the table can be defined. row.vars can either be the names of the dimensions (included percentages are named "idx") or numbers (1:3, where 1 is the first dimension of the table, 2 the second and 3 the percentages). Use Sort() if you want to have your table sorted by rows. MarginTable returns a list containing all the margin tables of a n-dimensional table along all dimensions. It does not much more than margin.table besides add percentages and do the job for all the dimensions.

References

Agresti, Alan (2007) Introduction to categorical data analysis. NY: John Wiley and Sons, Section 2.4.5

See Also

Freq, table, ftable, prop.table, addmargins There are similar functions in package sfsmisc printTable2 and package vcd table2d_summary, both lacking some of the flexibility we needed here.

Examples

Run this code
tab <- table(d.pizza$driver, d.pizza$area)

PercTable(tab=tab, col.vars=2)

PercTable(tab=tab, col.vars=2, margins=c(1,2))
PercTable(tab=tab, col.vars=2, margins=2)
PercTable(tab=tab, col.vars=2, margins=1)
PercTable(tab=tab, col.vars=2, margins=NULL)

PercTable(tab=tab, col.vars=2, rfrq="000")

# just the percentages without absolute values
PercTable(tab=tab, col.vars=2, rfrq="110", freq=FALSE)

# just the row percentages in percent format (pfmt = TRUE)
PercTable(tab, freq= FALSE, rfrq="010", pfmt=TRUE, digits=1)

# just the expected frequencies and the standard residuals
PercTable(tab=tab, rfrq="000", expected = TRUE, stdres = TRUE)


# rearrange output such that freq are inserted as columns instead of rows
PercTable(tab=tab, col.vars=c(3,2), rfrq="111")

# putting the cities in rows 
PercTable(tab=tab, col.vars=c(3,1), rfrq="100", margins=c(1,2))

# formula interface with subset
PercTable(driver ~ area, data=d.pizza, subset=wine_delivered==0)

# sort the table by rows, order first column (Zurich), then third, then row.names (0)
PercTable(tab=Sort(tab, ord=c(1,3,0)))

# the vector interface
PercTable(x=d.pizza$driver, y=d.pizza$area)
PercTable(x=d.pizza$driver, y=d.pizza$area, margins=c(1,2), rfrq="000", useNA="ifany")

# one dimensional x falls back to the function Freq()
PercTable(x=d.pizza$driver)

# the margin tables
MarginTable(Titanic)

Run the code above in your browser using DataLab