Percent: Pseudo-function to compute a statistic relative to a...

Description Usage Arguments Details Pseudo-functions See Also Examples

View source: R/pseudo.R

Description

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

Usage

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

Arguments

denom

How the reference set (the denominator in case of a percentage calculation) should be calculated. See below.

fn

The two argument function to calculate the statistic.

Details

The function fn will be called with two arguments. The first argument is the usual “value vector” of values corresponding to this cell in the table, and the second is another vector of reference values, determined by denom.

The default value of fn is the percent function, defined as function(x, y) 100*length(x)/length(y). This gives the ratio of the number of values in the current cell relative to the reference values, expressed as a percentage. Using fn = function(x, y) 100*sum(x)/sum(y) would give the percentage of the sum of the values in the current cell to the sum in the reference set.

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 .... The reference set will be the cases with equal values of all expressions in ... to those of the current cell. 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 the values marked TRUE are used, or anything else, which will be passed to fn as y.

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.

See Also

Arguments for a different way to specify a multiple argument analysis function.

Examples

1
2
3
4
5
6
7
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)))))

Example output

Loading required package: Hmisc
Loading required package: lattice
Loading required package: survival
Loading required package: Formula
Loading required package: ggplot2

Attaching package: 'Hmisc'

The following objects are masked from 'package:base':

    format.pval, units

                                          
         z                                
         E          F          All        
 x   y   All RowPct All RowPct All  RowPct
 A   c   133 50.00  133 50.00   266 100   
     d   116 48.13  125 51.87   241 100   
     All 249 49.11  258 50.89   507 100   
 B   c   119 47.98  129 52.02   248 100   
     d   133 54.29  112 45.71   245 100   
     All 252 51.12  241 48.88   493 100   
 All c   252 49.03  262 50.97   514 100   
     d   249 51.23  237 48.77   486 100   
     All 501 50.10  499 49.90  1000 100   
                                       
         z                             
         E         F         All       
 x   y   All xyPct All xyPct All  xyPct
 A   c   133 50.00 133 50.00  266 100  
     d   116 48.13 125 51.87  241 100  
     All 249 49.11 258 50.89  507 100  
 B   c   119 47.98 129 52.02  248 100  
     d   133 54.29 112 45.71  245 100  
     All 252 51.12 241 48.88  493 100  
 All c   252 49.03 262 50.97  514 100  
     d   249 51.23 237 48.77  486 100  
     All 501 50.10 499 49.90 1000 100  

tables documentation built on Oct. 23, 2020, 5:21 p.m.