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

# S3 method for default
PercTable(x, y = NULL, ...)

# S3 method for table
PercTable(tab, row.vars = NULL, col.vars = NULL, justify = "right",
          freq = TRUE, rfrq = "100", expected = FALSE, residuals = FALSE,
          stdres = FALSE, margins = NULL, digits = NULL, ...)

# S3 method for formula
PercTable(formula, data, subset, na.action, ...)

# S3 method for PercTable
print(x, vsep = NULL, ...)

Margins(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). If this is left to NULL the table structure will be preserved.

justify

either "left" or "right" for defining the alignment of the table cells.

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).

digits

integer. With how many digits shoud the relative frequencies be formatted? Default can be set by DescToolsOptions(digits=x).

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").

vsep

logical, defining if an empty row should be introduced between the table rows. Default is FALSE, if only a table with one single description (either frequencies or percents) should be returned and TRUE in any other case.

...

the dots are passed from PercTable.default() to PercTable.table() and from Margins to the function Freq.

Details

PercTable prints a 2-dimensional table. The absolute and relative frequencies are nested into one flat table by means of ftable. row.vars, resp. col.vars can be used to define the structure of the table. 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.

The style in which numbers are formatted is selected by Fmt() from the DescTools options. Absolute frequencies will use Fmt("abs") and Fmt("per") will do it for the percentages. The options can be changed with Fmt(abs=as.fmt(...)) which is basically a "fmt"-object containing any format information used in Format.

Margins() returns a list containing all the one dimensional margin tables of a n-dimensional table along the given dimensions. It uses margin.table() for all the dimensions and adds the appropriate percentages.

Value

Returns an object of class "ftable".

References

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

Author

Andri Signorell <andri@signorell.net>

See also

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

Examples

tab <- table(driver=d.pizza$driver, area=d.pizza$area)

PercTable(tab=tab, col.vars=2)
#>                                                
#>             area   Brent   Camden   Westminster
#> driver                                         
#>                                                
#> Butcher     freq      72        1            22
#>             perc    6.0%     0.1%          1.8%
#>                                                
#> Carpenter   freq      29       19           221
#>             perc    2.4%     1.6%         18.5%
#>                                                
#> Carter      freq     177       47             5
#>             perc   14.8%     3.9%          0.4%
#>                                                
#> Farmer      freq      19       87            11
#>             perc    1.6%     7.3%          0.9%
#>                                                
#> Hunter      freq     128        4            24
#>             perc   10.7%     0.3%          2.0%
#>                                                
#> Miller      freq       6       41            77
#>             perc    0.5%     3.4%          6.4%
#>                                                
#> Taylor      freq      42      142            20
#>             perc    3.5%    11.9%          1.7%
#>                                                

PercTable(tab=tab, col.vars=2, margins=c(1,2))
#>                                                       
#>             area   Brent   Camden   Westminster    Sum
#> driver                                                
#>                                                       
#> Butcher     freq      72        1            22     95
#>             perc    6.0%     0.1%          1.8%   8.0%
#>                                                       
#> Carpenter   freq      29       19           221    269
#>             perc    2.4%     1.6%         18.5%  22.5%
#>                                                       
#> Carter      freq     177       47             5    229
#>             perc   14.8%     3.9%          0.4%  19.2%
#>                                                       
#> Farmer      freq      19       87            11    117
#>             perc    1.6%     7.3%          0.9%   9.8%
#>                                                       
#> Hunter      freq     128        4            24    156
#>             perc   10.7%     0.3%          2.0%  13.1%
#>                                                       
#> Miller      freq       6       41            77    124
#>             perc    0.5%     3.4%          6.4%  10.4%
#>                                                       
#> Taylor      freq      42      142            20    204
#>             perc    3.5%    11.9%          1.7%  17.1%
#>                                                       
#> Sum         freq     473      341           380  1'194
#>             perc   39.6%    28.6%         31.8% 100.0%
#>                                                       
PercTable(tab=tab, col.vars=2, margins=2)
#>                                                
#>             area   Brent   Camden   Westminster
#> driver                                         
#>                                                
#> Butcher     freq      72        1            22
#>             perc    6.0%     0.1%          1.8%
#>                                                
#> Carpenter   freq      29       19           221
#>             perc    2.4%     1.6%         18.5%
#>                                                
#> Carter      freq     177       47             5
#>             perc   14.8%     3.9%          0.4%
#>                                                
#> Farmer      freq      19       87            11
#>             perc    1.6%     7.3%          0.9%
#>                                                
#> Hunter      freq     128        4            24
#>             perc   10.7%     0.3%          2.0%
#>                                                
#> Miller      freq       6       41            77
#>             perc    0.5%     3.4%          6.4%
#>                                                
#> Taylor      freq      42      142            20
#>             perc    3.5%    11.9%          1.7%
#>                                                
#> Sum         freq     473      341           380
#>             perc   39.6%    28.6%         31.8%
#>                                                
PercTable(tab=tab, col.vars=2, margins=1)
#>                                                      
#>             area   Brent   Camden   Westminster   Sum
#> driver                                               
#>                                                      
#> Butcher     freq      72        1            22    95
#>             perc    6.0%     0.1%          1.8%  8.0%
#>                                                      
#> Carpenter   freq      29       19           221   269
#>             perc    2.4%     1.6%         18.5% 22.5%
#>                                                      
#> Carter      freq     177       47             5   229
#>             perc   14.8%     3.9%          0.4% 19.2%
#>                                                      
#> Farmer      freq      19       87            11   117
#>             perc    1.6%     7.3%          0.9%  9.8%
#>                                                      
#> Hunter      freq     128        4            24   156
#>             perc   10.7%     0.3%          2.0% 13.1%
#>                                                      
#> Miller      freq       6       41            77   124
#>             perc    0.5%     3.4%          6.4% 10.4%
#>                                                      
#> Taylor      freq      42      142            20   204
#>             perc    3.5%    11.9%          1.7% 17.1%
#>                                                      
PercTable(tab=tab, col.vars=2, margins=NULL)
#>                                                
#>             area   Brent   Camden   Westminster
#> driver                                         
#>                                                
#> Butcher     freq      72        1            22
#>             perc    6.0%     0.1%          1.8%
#>                                                
#> Carpenter   freq      29       19           221
#>             perc    2.4%     1.6%         18.5%
#>                                                
#> Carter      freq     177       47             5
#>             perc   14.8%     3.9%          0.4%
#>                                                
#> Farmer      freq      19       87            11
#>             perc    1.6%     7.3%          0.9%
#>                                                
#> Hunter      freq     128        4            24
#>             perc   10.7%     0.3%          2.0%
#>                                                
#> Miller      freq       6       41            77
#>             perc    0.5%     3.4%          6.4%
#>                                                
#> Taylor      freq      42      142            20
#>             perc    3.5%    11.9%          1.7%
#>                                                

PercTable(tab=tab, col.vars=2, rfrq="000")
#>                                         
#> area        Brent   Camden   Westminster
#> driver                                  
#> Butcher        72        1            22
#> Carpenter      29       19           221
#> Carter        177       47             5
#> Farmer         19       87            11
#> Hunter        128        4            24
#> Miller          6       41            77
#> Taylor         42      142            20

# just the percentages without absolute values
PercTable(tab=tab, col.vars=2, rfrq="110", freq=FALSE)
#>                                                 
#>             area    Brent   Camden   Westminster
#> driver                                          
#>                                                 
#> Butcher     perc     6.0%     0.1%          1.8%
#>             p.row   75.8%     1.1%         23.2%
#>                                                 
#> Carpenter   perc     2.4%     1.6%         18.5%
#>             p.row   10.8%     7.1%         82.2%
#>                                                 
#> Carter      perc    14.8%     3.9%          0.4%
#>             p.row   77.3%    20.5%          2.2%
#>                                                 
#> Farmer      perc     1.6%     7.3%          0.9%
#>             p.row   16.2%    74.4%          9.4%
#>                                                 
#> Hunter      perc    10.7%     0.3%          2.0%
#>             p.row   82.1%     2.6%         15.4%
#>                                                 
#> Miller      perc     0.5%     3.4%          6.4%
#>             p.row    4.8%    33.1%         62.1%
#>                                                 
#> Taylor      perc     3.5%    11.9%          1.7%
#>             p.row   20.6%    69.6%          9.8%
#>                                                 

# just the row percentages in percent format (pfmt = TRUE)
PercTable(tab, freq= FALSE, rfrq="010", pfmt=TRUE, digits=1)
#>                                         
#> area        Brent   Camden   Westminster
#> driver                                  
#> Butcher     75.8%     1.1%         23.2%
#> Carpenter   10.8%     7.1%         82.2%
#> Carter      77.3%    20.5%          2.2%
#> Farmer      16.2%    74.4%          9.4%
#> Hunter      82.1%     2.6%         15.4%
#> Miller       4.8%    33.1%         62.1%
#> Taylor      20.6%    69.6%          9.8%

# just the expected frequencies and the standard residuals
PercTable(tab=tab, rfrq="000", expected = TRUE, stdres = TRUE)
#>                                                  
#>             area     Brent   Camden   Westminster
#> driver                                           
#>                                                  
#> Butcher     freq        72        1            22
#>             exp     37.634   27.131        30.235
#>             stdres   7.514   -6.187        -1.891
#>                                                  
#> Carpenter   freq        29       19           221
#>             exp    106.564   76.825        85.611
#>             stdres -10.985   -8.868        20.134
#>                                                  
#> Carter      freq       177       47             5
#>             exp     90.718   65.401        72.881
#>             stdres  12.967   -2.994       -10.712
#>                                                  
#> Farmer      freq        19       87            11
#>             exp     46.349   33.415        37.236
#>             stdres  -5.443   11.548        -5.483
#>                                                  
#> Hunter      freq       128        4            24
#>             exp     61.799   44.553        49.648
#>             stdres  11.623   -7.709        -4.728
#>                                                  
#> Miller      freq         6       41            77
#>             exp     49.122   35.414        39.464
#>             stdres  -8.364    1.173         7.644
#>                                                  
#> Taylor      freq        42      142            20
#>             exp     80.814   58.261        64.925
#>             stdres  -6.102   14.254        -7.416
#>                                                  


# rearrange output such that freq are inserted as columns instead of rows
PercTable(tab=tab, col.vars=c(3,2), rfrq="111")
#>                                                                                
#>              freq                           perc                          p.row
#> area        Brent   Camden   Westminster   Brent   Camden   Westminster   Brent
#> driver                                                                         
#> Butcher        72        1            22    6.0%     0.1%          1.8%   75.8%
#> Carpenter      29       19           221    2.4%     1.6%         18.5%   10.8%
#> Carter        177       47             5   14.8%     3.9%          0.4%   77.3%
#> Farmer         19       87            11    1.6%     7.3%          0.9%   16.2%
#> Hunter        128        4            24   10.7%     0.3%          2.0%   82.1%
#> Miller          6       41            77    0.5%     3.4%          6.4%    4.8%
#> Taylor         42      142            20    3.5%    11.9%          1.7%   20.6%
#>                                                                
#>                                    p.col                       
#> area        Camden   Westminster   Brent   Camden   Westminster
#> driver                                                         
#> Butcher       1.1%         23.2%   15.2%     0.3%          5.8%
#> Carpenter     7.1%         82.2%    6.1%     5.6%         58.2%
#> Carter       20.5%          2.2%   37.4%    13.8%          1.3%
#> Farmer       74.4%          9.4%    4.0%    25.5%          2.9%
#> Hunter        2.6%         15.4%   27.1%     1.2%          6.3%
#> Miller       33.1%         62.1%    1.3%    12.0%         20.3%
#> Taylor       69.6%          9.8%    8.9%    41.6%          5.3%

# putting the areas in rows
PercTable(tab=tab, col.vars=c(3,1), rfrq="100", margins=c(1,2))
#>                                                                               
#>                  freq                                                         
#> driver        Butcher   Carpenter   Carter   Farmer   Hunter   Miller   Taylor
#> area                                                                          
#> Brent              72          29      177       19      128        6       42
#> Camden              1          19       47       87        4       41      142
#> Westminster        22         221        5       11       24       77       20
#> Sum                95         269      229      117      156      124      204
#>                                                                             
#>                         perc                                                
#> driver         Sum   Butcher   Carpenter   Carter   Farmer   Hunter   Miller
#> area                                                                        
#> Brent          473      6.0%        2.4%    14.8%     1.6%    10.7%     0.5%
#> Camden         341      0.1%        1.6%     3.9%     7.3%     0.3%     3.4%
#> Westminster    380      1.8%       18.5%     0.4%     0.9%     2.0%     6.4%
#> Sum          1'194      8.0%       22.5%    19.2%     9.8%    13.1%    10.4%
#>                            
#>                            
#> driver        Taylor    Sum
#> area                       
#> Brent           3.5%  39.6%
#> Camden         11.9%  28.6%
#> Westminster     1.7%  31.8%
#> Sum            17.1% 100.0%

# formula interface with subset
PercTable(driver ~ area, data=d.pizza, subset=wine_delivered==0)
#>                                                
#>             area   Brent   Camden   Westminster
#> driver                                         
#>                                                
#> Butcher     freq      65        1            18
#>             perc    6.4%     0.1%          1.8%
#>                                                
#> Carpenter   freq      27       14           170
#>             perc    2.6%     1.4%         16.7%
#>                                                
#> Carter      freq     161       42             4
#>             perc   15.8%     4.1%          0.4%
#>                                                
#> Farmer      freq      19       72             9
#>             perc    1.9%     7.1%          0.9%
#>                                                
#> Hunter      freq     113        4            22
#>             perc   11.1%     0.4%          2.2%
#>                                                
#> Miller      freq       6       35            67
#>             perc    0.6%     3.4%          6.6%
#>                                                
#> Taylor      freq      36      118            18
#>             perc    3.5%    11.6%          1.8%
#>                                                

# sort the table by rows, order first column (Zurich), then third, then row.names (0)
PercTable(tab=Sort(tab, ord=c(1,3,0)))
#>                                                
#>             area   Brent   Camden   Westminster
#> driver                                         
#>                                                
#> Miller      freq       6       41            77
#>             perc    0.5%     3.4%          6.4%
#>                                                
#> Farmer      freq      19       87            11
#>             perc    1.6%     7.3%          0.9%
#>                                                
#> Carpenter   freq      29       19           221
#>             perc    2.4%     1.6%         18.5%
#>                                                
#> Taylor      freq      42      142            20
#>             perc    3.5%    11.9%          1.7%
#>                                                
#> Butcher     freq      72        1            22
#>             perc    6.0%     0.1%          1.8%
#>                                                
#> Hunter      freq     128        4            24
#>             perc   10.7%     0.3%          2.0%
#>                                                
#> Carter      freq     177       47             5
#>             perc   14.8%     3.9%          0.4%
#>                                                

# reverse the row variables, so that absolute frequencies and percents
# are not nested together
PercTable(tab, row.vars=c(3, 1))
#>                                                
#>        area        Brent   Camden   Westminster
#>        driver                                  
#> freq   Butcher        72        1            22
#>        Carpenter      29       19           221
#>        Carter        177       47             5
#>        Farmer         19       87            11
#>        Hunter        128        4            24
#>        Miller          6       41            77
#>        Taylor         42      142            20
#>                                                
#> perc   Butcher      6.0%     0.1%          1.8%
#>        Carpenter    2.4%     1.6%         18.5%
#>        Carter      14.8%     3.9%          0.4%
#>        Farmer       1.6%     7.3%          0.9%
#>        Hunter      10.7%     0.3%          2.0%
#>        Miller       0.5%     3.4%          6.4%
#>        Taylor       3.5%    11.9%          1.7%
#>                                                

# the vector interface
PercTable(x=d.pizza$driver, y=d.pizza$area)
#>                                                
#>                    Brent   Camden   Westminster
#>                                                
#> Butcher     freq      72        1            22
#>             perc    6.0%     0.1%          1.8%
#>                                                
#> Carpenter   freq      29       19           221
#>             perc    2.4%     1.6%         18.5%
#>                                                
#> Carter      freq     177       47             5
#>             perc   14.8%     3.9%          0.4%
#>                                                
#> Farmer      freq      19       87            11
#>             perc    1.6%     7.3%          0.9%
#>                                                
#> Hunter      freq     128        4            24
#>             perc   10.7%     0.3%          2.0%
#>                                                
#> Miller      freq       6       41            77
#>             perc    0.5%     3.4%          6.4%
#>                                                
#> Taylor      freq      42      142            20
#>             perc    3.5%    11.9%          1.7%
#>                                                
PercTable(x=d.pizza$driver, y=d.pizza$area, margins=c(1,2), rfrq="000", useNA="ifany")
#>                                                     
#>             Brent   Camden   Westminster    NA   Sum
#>                                                     
#> Butcher        72        1            22     1    96
#> Carpenter      29       19           221     3   272
#> Carter        177       47             5     5   234
#> Farmer         19       87            11     0   117
#> Hunter        128        4            24     0   156
#> Miller          6       41            77     1   125
#> Taylor         42      142            20     0   204
#> NA              1        3             1     0     5
#> Sum           474      344           381    10 1'209

# one dimensional x falls back to the function Freq()
PercTable(x=d.pizza$driver)
#>                        
#>             freq   perc
#>                        
#> Butcher       96   8.0%
#> Carpenter    272  22.6%
#> Carter       234  19.4%
#> Farmer       117   9.7%
#> Hunter       156  13.0%
#> Miller       125  10.4%
#> Taylor       204  16.9%

# the margin tables
Margins(Titanic)
#> $Class
#>    level  freq   perc  cumfreq  cumperc
#> 1    1st   325  14.8%      325    14.8%
#> 2    2nd   285  12.9%      610    27.7%
#> 3    3rd   706  32.1%    1'316    59.8%
#> 4   Crew   885  40.2%    2'201   100.0%
#> 
#> $Sex
#>     level   freq   perc  cumfreq  cumperc
#> 1    Male  1'731  78.6%    1'731    78.6%
#> 2  Female    470  21.4%    2'201   100.0%
#> 
#> $Age
#>    level   freq   perc  cumfreq  cumperc
#> 1  Child    109   5.0%      109     5.0%
#> 2  Adult  2'092  95.0%    2'201   100.0%
#> 
#> $Survived
#>    level   freq   perc  cumfreq  cumperc
#> 1     No  1'490  67.7%    1'490    67.7%
#> 2    Yes    711  32.3%    2'201   100.0%
#>