Learn R Programming

tables (version 0.7.79)

Percent: Percent pseudo-function

Description

The Percent pseudo-function is used to specify a statistic that depends on other values in the table.

Usage

Percent(denom = "all", fn = percent)

Arguments

denom
How the denominator should be calculated. See below.
fn
The two argument function to calculate the statistic.

Pseudo-functions

Percent is a “pseudo-function”: it takes the form of a function call, but is never actually called: it is handled specially by tabular. Equal and Unequal are also pseudo-functions, but are only special when used in the denom argument to Percent.

Details

The function fn will be called with two arguments. The first is the usual vector of values to which this statistic applies, and the second is another vector of reference values, determined by denom. The default function is percent, defined as function(x, y) 100*length(x)/length(y).

With the default denom = "all", all values of the analysis variable in the dataset are used as the reference. Other possibilities are denom = "row" or denom = "col", for which the values of the variable corresponding to the current row or column subset are used.

The special syntax denom = Equal(...) will record each expression in ... and ignore any factors that are not listed when forming the reference set. The similar form denom = Unequal(...) sets the reference values to be those that differ in any of the ... expressions from the current cell. (In fact, these can be used somewhat more generally; see the vignette for details.)

Finally, other possible denom values are a logical vector, in which case those values are used, or anything else, which will be passed to fn as y.

Examples

Run this code
x <- factor(sample(LETTERS[1:2], 1000, rep=TRUE))
y <- factor(sample(letters[3:4], 1000, rep=TRUE))
z <- factor(sample(LETTERS[5:6], 1000, rep=TRUE))

# These both do the same thing:
tabular( (x + 1)*(y + 1) ~ (z + 1)*(1+(RowPct=Percent("row"))))
tabular( (x + 1)*(y + 1) ~ (z + 1)*(1+(xyPct=Percent(Equal(x, y)))))

Run the code above in your browser using DataLab