ctab: Percentage tables

Description Usage Arguments Details Value Author(s) References See Also Examples

Description

Produces one-way, two-way or multi-way percentage tables

Usage

1
2
3
4
5
6
7
ctab(...,dec.places=NULL,digits=NULL,type=NULL,style=NULL,row.vars=NULL,col.vars=NULL,percentages=NULL,addmargins=NULL)

## S3 method for class 'ctab'
print(x, dec.places=x$dec.places, addmargins=x$addmargins, ...)

## S3 method for class 'ctab'
summary(object,...)

Arguments

...

either

  • one or more factors,

  • a class table

  • a class ftable table object,

  • or a class ctab table object

dec.places

number of decimal places (default 2)

digits

synonym for dec.places, for compatability with previous version

type

Row, column, total percentages or counts (type= n). Multiple values may be specified as a character vector. Partial matchin is used.

style

Applicable if more than one percentage type is specified. If style=long, percentages are printed underneath each other. If style=wide, the percentages are printed side by side

row.vars

Same as ftable: “a vector of integers giving the numbers of the variables, or a character vector giving the names of the variables to be used for the rows of the [] table”

col.vars

“a vector of integers giving the numbers of the variables, or a character vector giving the names of the variables to be used for the columns of the [] table”

percentages

If FALSE, proportions rather than percentages are printed

addmargins

Use addmargins=TRUE to add subtotals to the table

x

is a tables object created by ctab

object

is a tables object created by ctab

Details

Options have default NULL so attributes of a ctab object can be used as default. In other cases, the following options will be used if nothing has been specified:

1
2
ctab(\dots, dec.places=2, digits=dec.places, type=c("n", "row", "column", "total"),
style="long", row.vars=NULL, col.vars=NULL, percentages=TRUE, addmargins=FALSE)

ctab uses ftable and prop.table to produce one-way frequency tables, two-way crosstables, or multi-way percentage tables. More than one percentage type may be specified, in which case “percentage type” is an unnamed dimension of the table. row.vars and col.vars can be used to control the layout of multi-way tables using the facilities of ftable. Subtotals can be added by specifiying addmargins=TRUE.

CrossTable in the gmodels package also provides an easy method for producing percentage tables, but is restricted to two-way tables.

If ctab is specified with no further options and for more than one factor, the output is identical to that of ftable. If a single factor is specified, the default is to print the frequencies column-wise with the percentaqes next to them.

Value

An object of class “ctab”. print.ctab prints the table, summary.ctab passes the frequency table on to summary.table, which prints the number of cases, number of factors, and a chi-square test of independence.

table

A class(table) object containing the table counts. Used by summary.ctab and by ctab itself if a ctab object is used as input.

ctab

A class(ftable) object containing the percentage types specified. This is printed by print.ctab.

row.vars

The row.vars options as numeric vectors

col.vars

The col.vars options as numeric vectors

dec.places

The dec.places option

type

The type option

style

The style option

percentages

The percentages option

addmargins

The addmargins option

Author(s)

John Hendrickx <John_Hendrickx@yahoo.com>

References

http://home.wanadoo.nl/john.hendrickx/statres/

See Also

table, ftable, addmargins, prop.table, xtabs, [gmodels]CrossTable

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
ftable(Titanic)
ctab(Titanic) # same output
ctab(Titanic,type="r")
ctab(Titanic,type=c("n","r"),addmargins=TRUE)
ctab(Titanic,type=c("n","c","t","r"),style="w")
mytab<-ftable(Titanic,row.vars=c(1,3),type="r")
mytab
ctab(mytab)
newtab<-ctab(mytab,type="r")
newtab
summary(newtab)

#second example using a data frame rather than table data
library(survival)
data(logan)
attach(logan)
class(logan) #"data.frame"
ctab(occupation)
ctab(occupation,addmargins=TRUE)
ctab(occupation,style="w",type="c")
ctab(occupation,style="l",type="n")
z<-ctab(occupation,addmargins=TRUE,style="l")
z
print(z,addmargins=FALSE,dec.places=5)
summary(z)

t<-ctab(focc,occupation,type=c("n","r","c"))
t
summary(t)

Example output

                   Survived  No Yes
Class Sex    Age                   
1st   Male   Child            0   5
             Adult          118  57
      Female Child            0   1
             Adult            4 140
2nd   Male   Child            0  11
             Adult          154  14
      Female Child            0  13
             Adult           13  80
3rd   Male   Child           35  13
             Adult          387  75
      Female Child           17  14
             Adult           89  76
Crew  Male   Child            0   0
             Adult          670 192
      Female Child            0   0
             Adult            3  20
                   Survived  No Yes
Class Sex    Age                   
1st   Male   Child            0   5
             Adult          118  57
      Female Child            0   1
             Adult            4 140
2nd   Male   Child            0  11
             Adult          154  14
      Female Child            0  13
             Adult           13  80
3rd   Male   Child           35  13
             Adult          387  75
      Female Child           17  14
             Adult           89  76
Crew  Male   Child            0   0
             Adult          670 192
      Female Child            0   0
             Adult            3  20
                   Survived     No    Yes
Class Sex    Age                         
1st   Male   Child            0.00 100.00
             Adult           67.43  32.57
      Female Child            0.00 100.00
             Adult            2.78  97.22
2nd   Male   Child            0.00 100.00
             Adult           91.67   8.33
      Female Child            0.00 100.00
             Adult           13.98  86.02
3rd   Male   Child           72.92  27.08
             Adult           83.77  16.23
      Female Child           54.84  45.16
             Adult           53.94  46.06
Crew  Male   Child             NaN    NaN
             Adult           77.73  22.27
      Female Child             NaN    NaN
             Adult           13.04  86.96
                         Survived     No    Yes    Sum
Class Sex    Age                                      
1st   Male   Child Count            0.00   5.00   5.00
                   Row %            0.00 100.00 100.00
             Adult Count          118.00  57.00 175.00
                   Row %           67.43  32.57 100.00
             Sum   Count          118.00  62.00 180.00
                   Row %           67.43 132.57 200.00
      Female Child Count            0.00   1.00   1.00
                   Row %            0.00 100.00 100.00
             Adult Count            4.00 140.00 144.00
                   Row %            2.78  97.22 100.00
             Sum   Count            4.00 141.00 145.00
                   Row %            2.78 197.22 200.00
2nd   Male   Child Count            0.00  11.00  11.00
                   Row %            0.00 100.00 100.00
             Adult Count          154.00  14.00 168.00
                   Row %           91.67   8.33 100.00
             Sum   Count          154.00  25.00 179.00
                   Row %           91.67 108.33 200.00
      Female Child Count            0.00  13.00  13.00
                   Row %            0.00 100.00 100.00
             Adult Count           13.00  80.00  93.00
                   Row %           13.98  86.02 100.00
             Sum   Count           13.00  93.00 106.00
                   Row %           13.98 186.02 200.00
3rd   Male   Child Count           35.00  13.00  48.00
                   Row %           72.92  27.08 100.00
             Adult Count          387.00  75.00 462.00
                   Row %           83.77  16.23 100.00
             Sum   Count          422.00  88.00 510.00
                   Row %          156.68  43.32 200.00
      Female Child Count           17.00  14.00  31.00
                   Row %           54.84  45.16 100.00
             Adult Count           89.00  76.00 165.00
                   Row %           53.94  46.06 100.00
             Sum   Count          106.00  90.00 196.00
                   Row %          108.78  91.22 200.00
Crew  Male   Child Count            0.00   0.00   0.00
                   Row %            0.00   0.00   0.00
             Adult Count          670.00 192.00 862.00
                   Row %           77.73  22.27 100.00
             Sum   Count          670.00 192.00 862.00
                   Row %           77.73  22.27 100.00
      Female Child Count            0.00   0.00   0.00
                   Row %            0.00   0.00   0.00
             Adult Count            3.00  20.00  23.00
                   Row %           13.04  86.96 100.00
             Sum   Count            3.00  20.00  23.00
                   Row %           13.04  86.96 100.00
                             Count        Column %        Total %         Row %       
                   Survived     No    Yes       No    Yes      No    Yes     No    Yes
Class Sex    Age                                                                      
1st   Male   Child            0.00   5.00     0.00   8.06    0.00   2.78   0.00 100.00
             Adult          118.00  57.00   100.00  91.94   65.56  31.67  67.43  32.57
      Female Child            0.00   1.00     0.00   0.71    0.00   0.69   0.00 100.00
             Adult            4.00 140.00   100.00  99.29    2.76  96.55   2.78  97.22
2nd   Male   Child            0.00  11.00     0.00  44.00    0.00   6.15   0.00 100.00
             Adult          154.00  14.00   100.00  56.00   86.03   7.82  91.67   8.33
      Female Child            0.00  13.00     0.00  13.98    0.00  12.26   0.00 100.00
             Adult           13.00  80.00   100.00  86.02   12.26  75.47  13.98  86.02
3rd   Male   Child           35.00  13.00     8.29  14.77    6.86   2.55  72.92  27.08
             Adult          387.00  75.00    91.71  85.23   75.88  14.71  83.77  16.23
      Female Child           17.00  14.00    16.04  15.56    8.67   7.14  54.84  45.16
             Adult           89.00  76.00    83.96  84.44   45.41  38.78  53.94  46.06
Crew  Male   Child            0.00   0.00     0.00   0.00    0.00   0.00   0.00   0.00
             Adult          670.00 192.00   100.00 100.00   77.73  22.27  77.73  22.27
      Female Child            0.00   0.00     0.00   0.00    0.00   0.00   0.00   0.00
             Adult            3.00  20.00   100.00 100.00   13.04  86.96  13.04  86.96
            Sex      Male     Female    
            Survived   No Yes     No Yes
Class Age                               
1st   Child             0   5      0   1
      Adult           118  57      4 140
2nd   Child             0  11      0  13
      Adult           154  14     13  80
3rd   Child            35  13     17  14
      Adult           387  75     89  76
Crew  Child             0   0      0   0
      Adult           670 192      3  20
            Sex      Male     Female    
            Survived   No Yes     No Yes
Class Age                               
1st   Child             0   5      0   1
      Adult           118  57      4 140
2nd   Child             0  11      0  13
      Adult           154  14     13  80
3rd   Child            35  13     17  14
      Adult           387  75     89  76
Crew  Child             0   0      0   0
      Adult           670 192      3  20
            Sex        Male        Female       
            Survived     No    Yes     No    Yes
Class Age                                       
1st   Child            0.00 100.00   0.00 100.00
      Adult           67.43  32.57   2.78  97.22
2nd   Child            0.00 100.00   0.00 100.00
      Adult           91.67   8.33  13.98  86.02
3rd   Child           72.92  27.08  54.84  45.16
      Adult           83.77  16.23  53.94  46.06
Crew  Child             NaN    NaN    NaN    NaN
      Adult           77.73  22.27  13.04  86.96
Number of cases in table: 2201 
Number of factors: 4 
Test for independence of all factors:
	Chisq = 1637.4, df = 25, p-value = 0
	Chi-squared approximation may be incorrect
[1] "data.frame"
               Count Total %
occupation                  
farm           19.00    2.27
operatives    217.00   25.89
craftsmen     202.00   24.11
sales         105.00   12.53
professional  295.00   35.20
               Count Total %
occupation                  
farm           19.00    2.27
operatives    217.00   25.89
craftsmen     202.00   24.11
sales         105.00   12.53
professional  295.00   35.20
Sum           838.00  100.00
occupation
        farm   operatives    craftsmen        sales professional 
        2.27        25.89        24.11        12.53        35.20 
             occupation
farm                 19
operatives          217
craftsmen           202
sales               105
professional        295
occupation                  
farm         Count     19.00
             Total %    2.27
operatives   Count    217.00
             Total %   25.89
craftsmen    Count    202.00
             Total %   24.11
sales        Count    105.00
             Total %   12.53
professional Count    295.00
             Total %   35.20
Sum          Count    838.00
             Total %  100.00
occupation                     
farm         Count     19.00000
             Total %    2.26730
operatives   Count    217.00000
             Total %   25.89499
craftsmen    Count    202.00000
             Total %   24.10501
sales        Count    105.00000
             Total %   12.52983
professional Count    295.00000
             Total %   35.20286
Number of cases in table: 838 
Number of factors: 1 
                      occupation   farm operatives craftsmen  sales professional
focc                                                                            
farm         Count                15.00      29.00     26.00   7.00        15.00
             Row %                16.30      31.52     28.26   7.61        16.30
             Column %             78.95      13.36     12.87   6.67         5.08
operatives   Count                 2.00      94.00     54.00  27.00        58.00
             Row %                 0.85      40.00     22.98  11.49        24.68
             Column %             10.53      43.32     26.73  25.71        19.66
craftsmen    Count                 1.00      55.00     79.00  27.00        70.00
             Row %                 0.43      23.71     34.05  11.64        30.17
             Column %              5.26      25.35     39.11  25.71        23.73
sales        Count                 0.00      15.00     16.00   8.00        43.00
             Row %                 0.00      18.29     19.51   9.76        52.44
             Column %              0.00       6.91      7.92   7.62        14.58
professional Count                 1.00      24.00     27.00  36.00       109.00
             Row %                 0.51      12.18     13.71  18.27        55.33
             Column %              5.26      11.06     13.37  34.29        36.95
Number of cases in table: 838 
Number of factors: 2 
Test for independence of all factors:
	Chisq = 201.44, df = 16, p-value = 4.061e-34
	Chi-squared approximation may be incorrect

catspec documentation built on May 1, 2019, 8:21 p.m.

Related to ctab in catspec...